TomlValue structv0.1.0
pub struct TomlValue {
pub kind: TomlKind;
pub integer: int64;
// private fields
}
Move-only, because it owns strings and nested values. The allocator it was built with travels with it, so a subtree can be released without the caller having to remember where it came from.
Fields
| Name | Type | Description |
|---|---|---|
kind | TomlKind | Which of the ten kinds this is. |
integer | int64 | The payload of an Integer. |
Associated functions
Table
pub func Table(allocator: Allocator) -> TomlValue
An empty table, which is also the shape every other kind is built from: TOML has no null to start from.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: an empty table
Array
pub func Array(allocator: Allocator) -> TomlValue
An empty array.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
Returns: an empty array
Text
pub func Text(
allocator: Allocator,
value: char8[..]
) -> TomlValue ! TextError
A string value, whose bytes are copied and validated as UTF-8.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | char8[..] | the value to act on |
Returns: that text as a string value, or an empty table when the allocator refused
FromString
pub func FromString(
allocator: Allocator,
value: String
) -> TomlValue
A string value copying an already-built string.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | String | the value to act on |
Returns: that owned string as a string value
Integer
pub func Integer(
allocator: Allocator,
value: int64
) -> TomlValue
An integer value.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | int64 | the value to act on |
Returns: that integer as a value
Float
pub func Float(
allocator: Allocator,
value: float64
) -> TomlValue
A float value, which TOML — unlike JSON — allows to be an infinity or a NaN.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | float64 | the value to act on |
Returns: that float as a value
Boolean
pub func Boolean(
allocator: Allocator,
value: bool
) -> TomlValue
A boolean value.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
value | bool | the value to act on |
Returns: that boolean as a value
OffsetDateTime
pub func OffsetDateTime(
allocator: Allocator,
date: Date,
time: TimeOfDay,
offset: UtcOffset
) -> TomlValue
A date and time with a known offset, which names a moment on the timeline.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
date | Date | the calendar date |
time | TimeOfDay | the time of day |
offset | UtcOffset | the offset from UTC |
Returns: a value carrying the date, the time and the offset from UTC
LocalDateTime
pub func LocalDateTime(
allocator: Allocator,
date: Date,
time: TimeOfDay
) -> TomlValue
A date and time with no offset, which names a wall clock reading rather than a moment.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
date | Date | the calendar date |
time | TimeOfDay | the time of day |
Returns: a value carrying the date and the time, with no offset
LocalDate
pub func LocalDate(
allocator: Allocator,
date: Date
) -> TomlValue
A date with no time.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
date | Date | the calendar date |
Returns: a value carrying the date alone
LocalTime
pub func LocalTime(
allocator: Allocator,
time: TimeOfDay
) -> TomlValue
A time with no date.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
time | TimeOfDay | the time of day |
Returns: a value carrying the time alone
Methods
Kind
pub func Kind(self: &TomlValue) -> TomlKind
Which kind this value is.
Returns: which of the TOML kinds this is
AsBoolean
pub func AsBoolean(
self: &TomlValue,
result: *var bool
) -> bool
The boolean payload, or false when this is not a boolean — which Kind distinguishes from a genuine
false.
| Name | Type | Description |
|---|---|---|
result | *var bool | receives the value when this reports true |
Returns: whether this is a boolean
AsInteger
pub func AsInteger(
self: &TomlValue,
result: *var int64
) -> bool
The integer payload, or false when this is not an integer.
A float is not converted, even when its value happens to be whole: TOML distinguishes 1 from 1.0 and
collapsing them here would throw away a distinction the document was making.
| Name | Type | Description |
|---|---|---|
result | *var int64 | receives the value when this reports true |
Returns: whether this is an integer
AsFloat
pub func AsFloat(
self: &TomlValue,
result: *var float64
) -> bool
The float payload, or false when this is not a float.
| Name | Type | Description |
|---|---|---|
result | *var float64 | receives the value when this reports true |
Returns: whether this is a float
AsText
pub func AsText(self: &TomlValue) -> StringView
The string payload, or an empty view when this is not a string.
Returns: the string, or an empty view when this is not one
AsDate
pub func AsDate(self: &TomlValue, result: *var Date) -> bool
The date part, or false when this kind has none.
| Name | Type | Description |
|---|---|---|
result | *var Date | receives the value when this reports true |
Returns: whether this carries a date
AsTime
pub func AsTime(
self: &TomlValue,
result: *var TimeOfDay
) -> bool
The time part, or false when this kind has none.
| Name | Type | Description |
|---|---|---|
result | *var TimeOfDay | receives the value when this reports true |
Returns: whether this carries a time
AsOffset
pub func AsOffset(
self: &TomlValue,
result: *var UtcOffset
) -> bool
The offset, or false when this is not an offset date-time.
A local date-time deliberately has none: it names a wall clock reading, and inventing an offset for it is
how a meeting ends up an hour out.
| Name | Type | Description |
|---|---|---|
result | *var UtcOffset | receives the value when this reports true |
Returns: whether this carries an offset from UTC
Length
pub func Length(self: &TomlValue) -> uint
How many elements an array holds, or entries a table holds; zero for anything else.
Returns: how many elements or entries this holds; zero for anything else
Push
pub func Push(
self: &var TomlValue,
value: TomlValue
) -> bool
Appends value to an array, taking ownership of it.
Reports false when this is not an array or the allocator refused; value is consumed either way, so a
caller does not have to decide who owns it on failure.
| Name | Type | Description |
|---|---|---|
value | TomlValue | the value to act on |
Returns: whether the element was appended
Insert
pub func Insert(
self: &var TomlValue,
key: String,
value: TomlValue
) -> bool
Appends an entry to a table, copying the key and taking ownership of the value. The order entries are added in is the order they are kept and written, which is what makes a document round-trip recognisably. Nothing here rejects a repeated key — that is the parser's business, where the three different ways of repeating one can be told apart.
| Name | Type | Description |
|---|---|---|
key | String | the key to look up |
value | TomlValue | the value to act on |
Returns: whether the entry was added
At
pub func At(self: &TomlValue, index: uint) -> *TomlValue
The element at index, borrowed, or null when there is none.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the element at index, or null when there is none
AtMutable
pub func AtMutable(
self: &var TomlValue,
index: uint
) -> *var TomlValue
The element at index, borrowed for modification, or null when there is none.
This is how an array of tables grows: the header that opens a new one appends, and the lines under it
are written into the last element in place.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the element at index for writing, or null when there is none
Find
pub func Find(
self: &TomlValue,
key: StringView
) -> *TomlValue
The value under key, borrowed, or null when the table has none.
| Name | Type | Description |
|---|---|---|
key | StringView | the key to look up |
Returns: the entry with that key, or null when there is none
FindMutable
pub func FindMutable(
self: &var TomlValue,
key: StringView
) -> *var TomlValue
The value under key, borrowed for modification, or null when the table has none.
| Name | Type | Description |
|---|---|---|
key | StringView | the key to look up |
Returns: the entry with that key for writing, or null when there is none
EntryAt
pub func EntryAt(
self: &TomlValue,
index: uint
) -> *TomlEntry
The entry at index, borrowed, or null when there is none.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the entry at index, or null when there is none
Destructor
func ~TomlValue(self: &var TomlValue)
Releases everything this value owns. Written out rather than left to the compiler, because a value owns its string and its children own theirs: the recursion here is the shape of the document, and stating it makes the ownership of a nested tree something a reader can check.
Related
TomlKind
pub enum TomlKind: int32 {
Text = 0,
Integer = 1,
Float = 2,
Boolean = 3,
OffsetDateTime = 4,
LocalDateTime = 5,
LocalDate = 6,
LocalTime = 7,
Array = 8,
Table = 9
}
Which kind of TOML value this is.
Cases
| Name | Value | Description |
|---|---|---|
Text | 0 | A string, whichever of the four forms it was written in. |
Integer | 1 | A 64-bit signed integer. |
Float | 2 | A 64-bit float, which unlike JSON may be an infinity or a NaN. |
Boolean | 3 | true or false. |
OffsetDateTime | 4 | A date and time with a known offset from UTC, which names a moment. |
LocalDateTime | 5 | A date and time with no offset, which names a reading of a wall clock. |
LocalDate | 6 | A date with no time. |
LocalTime | 7 | A time with no date. |
Array | 8 | An ordered sequence of values. |
Table | 9 | A table of entries, in the order they were seen. |
TomlEntry
pub struct TomlEntry {
pub key: String;
pub value: TomlValue;
}
One entry of a table: its key and its value.
Fields
| Name | Type | Description |
|---|---|---|
key | String | The key, already unquoted and unescaped. |
value | TomlValue | The value. |
Destructor
~TomlEntry
func ~TomlEntry(self: &var TomlEntry)
Releases the key and the value.