silk.toml.codec.
Convert TOML values to Silk values, and write Silk values as TOML.
When to use#
After silk.toml.value parses a document, use TomlCodec.decodeOne to read a bool, i32,
or i64, and TomlCodec.deserializeOne to read an f64 or an owned String. Use
TomlCodec.writeOne to write a value after a key when you write TOML by hand.
Details#
| Type | Write | Read |
|---|---|---|
bool | Serialize | Decode from Boolean |
i32, i64 | Serialize | Decode from Integer, with 0x, 0o, 0b and _ |
f64 | Serialize | Deserialize from Float, with inf and nan |
string, String | Serialize | Deserialize from String (only String) |
To support your own type, implement the interfaces for it.
Errors#
TomlErrorwithTypeMismatchif the value has a different kind, or withNumberOutOfRangeif the number does not fit the type. AnIntegeris not converted tof64. All codec errors haveoffset0, because the value is not tied to a position in the source.WriterErrorif theWriterfails.OutOfMemoryErrorif theAllocatorcannot supply the output ofdeserializeOne.
Examples#
Read typed settings#
import silk.mem.allocator { Allocator, OutOfMemoryError }
import silk.core.effect { Effect }
import silk.core.option { Option }
import silk.core.result { Result }
import silk.toml.codec { TomlCodec }
import silk.toml.scanner { TomlError }
import silk.toml.value { Toml, Value }
fn port(document: &Value) -> i32 {
return match Toml.field(document, "port") {
Option<&Value>.Some { value } => match TomlCodec.decodeOne<i32>(value) {
Result<i32, TomlError>.Success { value: number } => number
Result<i32, TomlError>.Failure { error: _ } => 0
}
Option<&Value>.None => 0
}
}
pub effect fn main() -> i32
! TomlError | OutOfMemoryError {
let mut allocator = Allocator.systemAllocatorProvider()
let document = run Toml.parse(b"port = 8_080\nratio = 0.5\n")
|> Effect.provideMut<Allocator>(&mut allocator)
let ratio = match Toml.field(&document, "ratio") {
Option<&Value>.Some { value } => run TomlCodec.deserializeOne<f64>(value)
|> Effect.provideMut<Allocator>(&mut allocator)
Option<&Value>.None => 0.0
}
if port(&document) != 8080 || ratio != 0.5 {
return 1
}
return 0
}Import as TomlCodec with import silk.toml.codec { TomlCodec }.
Public declarations: 4.
Serialize#
pub interface SerializeA type that can write itself as one TOML value.
The standard library implements it for bool, i32, i64, f64, string, and String.
Implement it for your own type to write the type with TomlCodec.writeOne.
Operation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterWrites self as a TOML value through the ambient Writer. It does not write a key or a
newline.
Errors#
WriterErrorif theWriterfails.TomlErrorif the value has no TOML form.
Decode#
pub interface DecodeA type that can read itself from a TOML value without allocation.
The standard library implements it for bool (from Boolean), and i32 and i64 (from
Integer, with 0x, 0o, 0b and _). Read it with TomlCodec.decodeOne.
Operation decode#
fn decode(value: &Value) -> Result<Self, TomlError>Returns the value that value holds, or a TomlError if it is of a different kind or out of
range.
Deserialize#
pub interface DeserializeA type that can read itself from a TOML value and allocate.
The standard library implements it for f64 (from Float, with inf and nan; an Integer
is not converted) and String (from String). Read it with TomlCodec.deserializeOne.
Operation deserialize#
effect fn deserialize(value: &Value) -> Self ! TomlError | OutOfMemoryError ? &mut AllocatorReturns the value that value holds.
Errors#
TomlErrorifvalueis of a different kind or out of range.OutOfMemoryErrorif theAllocatorfails.
TomlCodec#
pub struct TomlCodecFunctions that select the conversion for a type.
Implementation TomlCodec#
impl TomlCodecAssociated function writeOne#
pub effect fn writeOne<T: Serialize>(value: &T) -> () ! TomlError | WriterError ? &mut WriterWrites value as a TOML value through the ambient Writer.
An f64 without a fraction gets .0, so it reads back as a float. NaN is written as nan
and infinity as inf or -inf. Strings are written as basic strings with escapes.
Errors#
WriterErrorif theWriterfails.TomlErrorif the type rejects the value.
Gotchas#
It writes only the value. Write the key and = before it, and a newline after it.
Associated function decodeOne#
pub fn decodeOne<T: Decode>(value: &Value) -> Result<T, TomlError>Returns value converted to T without allocation.
Returns a Failure with TypeMismatch if value has a different kind, or with
NumberOutOfRange if the number does not fit T.
Associated function deserializeOne#
pub effect fn deserializeOne<T: Deserialize>(value: &Value)
-> T ! TomlError | OutOfMemoryError ? &mut AllocatorReturns value converted to T, with memory from the ambient Allocator.
Errors#
TomlErrorwithTypeMismatchifvaluehas a different kind, or withNumberOutOfRangeif the number does not fitT.OutOfMemoryErrorif theAllocatorfails.
Implementation Serialize for bool#
impl Serialize for boolOperation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterImplementation Decode for bool#
impl Decode for boolOperation decode#
fn decode(value: &Value) -> Result<Self, TomlError>Implementation Serialize for i64#
impl Serialize for i64Operation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterImplementation Decode for i64#
impl Decode for i64Operation decode#
fn decode(value: &Value) -> Result<Self, TomlError>Implementation Serialize for i32#
impl Serialize for i32Operation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterImplementation Decode for i32#
impl Decode for i32Operation decode#
fn decode(value: &Value) -> Result<Self, TomlError>Implementation Serialize for f64#
impl Serialize for f64Operation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterImplementation Deserialize for f64#
impl Deserialize for f64Operation deserialize#
effect fn deserialize(value: &Value) -> Self ! TomlError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for string<'a>#
impl<'a> Serialize for string<'a>Operation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterImplementation Serialize for String#
impl Serialize for StringOperation serialize#
effect fn serialize(self: &Self) -> () ! TomlError | WriterError ? &mut WriterImplementation Deserialize for String#
impl Deserialize for StringOperation deserialize#
effect fn deserialize(value: &Value) -> Self ! TomlError | OutOfMemoryError ? &mut AllocatorCategory: Data formats.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.