TomlValue structv0.1.0
pub struct TomlValue {
// 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.
The kind is private: it is read with Kind and set only together with its payload, so the two never disagree,
and the accessor for another kind returns none rather than reinterpreting the bytes.
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 throws 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 InvalidUtf8 when it is not text
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) -> TomlKind
Which kind this value is.
Returns: which of the TOML kinds this is
AsBoolean
pub func AsBoolean(&self) -> bool?
The boolean payload, or none when this is not a boolean.
Returns: the boolean, or none when this is not one
AsInteger
pub func AsInteger(&self) -> int64?
The integer payload, or none 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.
Returns: the integer, or none when this is not one
AsFloat
pub func AsFloat(&self) -> float64?
The float payload, or none when this is not a float.
Returns: the float, or none when this is not one
AsText
pub func AsText(&self) -> StringView?
The string payload, or none when this is not a string.
Returns: the string, or none when this is not one
AsDate
pub func AsDate(&self) -> Date?
The date part, or none when this kind has none.
Returns: the date, or none when this value carries none
AsTime
pub func AsTime(&self) -> TimeOfDay?
The time part, or none when this kind has none.
Returns: the time of day, or none when this value carries none
AsOffset
pub func AsOffset(&self) -> UtcOffset?
The offset, or none 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.
Returns: the offset from UTC, or none when this value carries none
Length
pub func Length(&self) -> uint?
How many elements an array holds, or entries a table holds; none for any other kind.
Returns: how many elements or entries this holds, or none when it is neither an array nor a table
Push
pub func Push(&var self, value: TomlValue) throws TomlError
Appends value to an array, taking ownership of it.
Throws WrongKind when this is not an array; value is consumed either way, so a caller does not have to
decide who owns it on failure. Terminates the program when the array cannot grow.
| Name | Type | Description |
|---|---|---|
value | TomlValue | the value to act on |
Insert
pub func Insert(
&var self,
key: String,
value: TomlValue
) throws TomlError
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.
Throws WrongKind when this is not a table; key and value are consumed either way. Terminates the program
when the table cannot grow.
| Name | Type | Description |
|---|---|---|
key | String | the key to look up |
value | TomlValue | the value to act on |
At
pub func At(&self, index: uint) -> &TomlValue?
The element at index, borrowed, or none when there is none.
self stays borrowed while the reference is in use.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the element at index, or none when there is none
AtMutable
pub func AtMutable(
&var self,
index: uint
) -> &var TomlValue?
The element at index, borrowed for modification, or none 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 none when there is none
Find
pub func Find(&self, key: StringView) -> &TomlValue?
The value under key, borrowed, or none when the table has none.
| Name | Type | Description |
|---|---|---|
key | StringView | the key to look up |
Returns: the value under that key, or none when there is none
FindMutable
pub func FindMutable(
&var self,
key: StringView
) -> &var TomlValue?
The value under key, borrowed for modification, or none when the table has none.
| Name | Type | Description |
|---|---|---|
key | StringView | the key to look up |
Returns: the value under that key for writing, or none when there is none
EntryAt
pub func EntryAt(&self, index: uint) -> &TomlEntry?
The entry at index, borrowed, or none when there is none.
| Name | Type | Description |
|---|---|---|
index | uint | which one, counting from zero |
Returns: the entry at index, or none when there is none
Destructor
func ~TomlValue(&var self)
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(&var self)
Releases the key and the value.
TomlError
pub enum TomlError: int32 {
WrongKind = 1
}
Why a TOML value could not be changed. Running out of memory is not one of the reasons: a value whose storage cannot be had terminates the program.
Cases
| Name | Value | Description |
|---|---|---|
WrongKind | 1 | An operation for one kind of value asked of another, such as pushing onto a table. |