Skip to main content

Crate deser_toml

Crate deser_toml 

Source
Expand description

Parse and serialize TOML compatible with deser.

use deser::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
struct Config {
    name: String,
    ports: Vec<u16>,
}

let config: Config = deser_toml::from_str(r#"
name = "web"
ports = [80, 443]
"#).unwrap();
assert_eq!(config.name, "web");
assert_eq!(config.ports, [80, 443]);

let toml = deser_toml::to_string(&config).unwrap();
assert_eq!(toml, "name = \"web\"\nports = [80, 443]\n");

§Data Model

The parser implements TOML 1.1 and passes the toml-test suite. TOML maps onto the deser data model as follows:

TOMLdeser
tables (including the document)maps
arrays (including arrays of tables)sequences
stringsStr
integersU64, I64
floatsF64
booleansBool
date-times, dates and timesDatetime

Date-times are passed through deser as the well-known Datetime extension type which falls back to a string for types that do not know it. With the respective features of deser, the date and time types of jiff, chrono and time can be used directly (this example requires the jiff feature of deser):

use deser::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Event {
    start: jiff::Timestamp,
    day: jiff::civil::Date,
}

let event: Event = deser_toml::from_str("
start = 2024-06-19 15:22:45-04:00
day = 2024-06-19
").unwrap();
assert_eq!(event.start.to_string(), "2024-06-19T19:22:45Z");

let toml = deser_toml::to_string(&event).unwrap();
assert_eq!(toml, "start = 2024-06-19T19:22:45Z\nday = 2024-06-19\n");

The document is always a table. Keys are emitted in the order in which they are defined in the document.

Integers are supported in the range of i64 and u64, integers that do not fit are an error. Floats that overflow to infinity are an error as well. Newlines in multi-line strings are normalized to \n. A UTF-8 byte order mark at the start of the document is ignored.

When serializing, maps are written as tables and sequences of maps as arrays of tables. The well-known Timestamp type is written as offset date-time in UTC, other well-known types (such as UUIDs and decimals) are written as strings. Floats are written with the shortest text that reads back as the same value of their precision (0.1f32 as 0.1). TOML has no null value: map entries with null values are skipped and null values in sequences are an error. TOML has no bytes either, they are written as base64 strings by default (see bytes). See SerializerConfig for more information.

§Streams

Documents are read from a Read with from_reader and written to a Write with to_writer. The configurations also create readers and writers of deser::io (DeserializerConfig::reader and SerializerConfig::writer), the stream serializer (Serializer) and deserializer (StreamDeserializer) work with other kinds of IO too (for instance async runtimes with deser-tokio). As TOML documents cannot be split, the whole document is read before it’s parsed.

§Features

  • io (enabled by default): reading and writing streams of the standard library, see streams.
  • speedups (enabled by default): validates UTF-8 with simdutf8.

Structs§

Date
Re-exported from deser::ext for convenience. A calendar date (1979-05-27).
Datetime
Re-exported from deser::ext for convenience. A date, a time of the day, or both with an optional offset.
Deserializer
Deserializes TOML.
DeserializerConfig
Configures how TOML is deserialized.
DeserializerConfigBuilder
Builds a DeserializerConfig.
Serializer
Serializes values into TOML.
SerializerConfig
Configures how values are serialized to TOML.
SerializerConfigBuilder
Builds a SerializerConfig.
StreamDeserializer
Reads a TOML document from a stream (see deser::stream).
Time
Re-exported from deser::ext for convenience. A time of the day (07:32:00.999).

Enums§

Offset
Re-exported from deser::ext for convenience. The offset of an offset date-time.

Functions§

from_reader
Deserializes a document from a reader.
from_slice
Deserializes a value from TOML in a byte slice.
from_str
Deserializes a value from TOML.
to_string
Serializes a value to TOML.
to_writer
Serializes a value to a writer.