#toml-config #toml-parser #serialize-deserialize #codec #serialization #encoding #decoder

toml

A native Rust encoder and decoder of TOML-formatted files and streams. Provides implementations of the standard Serialize/Deserialize traits for TOML data to facilitate deserializing and serializing Rust structures.

85 releases

0.8.19 Jul 31, 2024
0.8.14 Jun 3, 2024
0.8.12 Mar 18, 2024
0.8.8 Nov 6, 2023
0.1.3 Nov 22, 2014

#7 in Parser implementations

Download history 2306655/week @ 2024-07-31 2454224/week @ 2024-08-07 2343803/week @ 2024-08-14 2470143/week @ 2024-08-21 2361684/week @ 2024-08-28 2562692/week @ 2024-09-04 2364344/week @ 2024-09-11 2443771/week @ 2024-09-18 2602173/week @ 2024-09-25 2970239/week @ 2024-10-02 2986354/week @ 2024-10-09 3047803/week @ 2024-10-16 2594134/week @ 2024-10-23 2446330/week @ 2024-10-30 2442020/week @ 2024-11-06 2140904/week @ 2024-11-13

10,117,351 downloads per month
Used in 17,594 crates (5,111 directly)

MIT/Apache

270KB
6K SLoC

toml

Latest Version Documentation

A serde-compatible TOML decoder and encoder for Rust.

For format-preserving edits or finer control over output, see toml_edit

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in toml-rs by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

A [serde]-compatible [TOML]-parsing library

TOML itself is a simple, ergonomic, and readable configuration format:

[package]
name = "toml"

[dependencies]
serde = "1.0"

The TOML format tends to be relatively common throughout the Rust community for configuration, notably being used by [Cargo], Rust's package manager.

TOML values

A TOML document is represented with the Table type which maps String to the Value enum:

pub enum Value {
    String(String),
    Integer(i64),
    Float(f64),
    Boolean(bool),
    Datetime(Datetime),
    Array(Array),
    Table(Table),
}

Parsing TOML

The easiest way to parse a TOML document is via the Table type:

use toml::Table;

let value = "foo = 'bar'".parse::

().unwrap();

assert_eq!(value["foo"].as_str(), Some("bar"));


The [`Table`] type implements a number of convenience methods and
traits; the example above uses [`FromStr`] to parse a [`str`] into a
[`Table`].

## Deserialization and Serialization

This crate supports [`serde`] 1.0 with a number of
implementations of the `Deserialize`, `Serialize`, `Deserializer`, and
`Serializer` traits. Namely, you'll find:

* `Deserialize for Table`
* `Serialize for Table`
* `Deserialize for Value`
* `Serialize for Value`
* `Deserialize for Datetime`
* `Serialize for Datetime`
* `Deserializer for de::Deserializer`
* `Serializer for ser::Serializer`
* `Deserializer for Table`
* `Deserializer for Value`

This means that you can use Serde to deserialize/serialize the
[`Table`] type as well as [`Value`] and [`Datetime`] type in this crate. You can also
use the [`Deserializer`], [`Serializer`], or [`Table`] type itself to act as
a deserializer/serializer for arbitrary types.

An example of deserializing with TOML is:

use serde::Deserialize;

#[derive(Deserialize)]
struct Config {
    ip: String,
    port: Option<u16>,
    keys: Keys,
}

#[derive(Deserialize)]
struct Keys {
    github: String,
    travis: Option<String>,
}

let config: Config = toml::from_str(r#"
    ip = '127.0.0.1'

    [keys]
    github = 'xxxxxxxxxxxxxxxxx'
    travis = 'yyyyyyyyyyyyyyyyy'
"#).unwrap();

assert_eq!(config.ip, "127.0.0.1");
assert_eq!(config.port, None);
assert_eq!(config.keys.github, "xxxxxxxxxxxxxxxxx");
assert_eq!(config.keys.travis.as_ref().unwrap(), "yyyyyyyyyyyyyyyyy");

You can serialize types in a similar fashion:

use serde::Serialize;

#[derive(Serialize)] struct Config { ip: String, port: Option, keys: Keys, }

#[derive(Serialize)] struct Keys { github: String, travis: Option, }

let config = Config { ip: "127.0.0.1".to_string(), port: None, keys: Keys { github: "xxxxxxxxxxxxxxxxx".to_string(), travis: Some("yyyyyyyyyyyyyyyyy".to_string()), }, };

let toml = toml::to_string(&config).unwrap();


[TOML]: https://github.com/toml-lang/toml
[Cargo]: https://crates.io/
[`serde`]: https://serde.rs/
[serde]: https://serde.rs/

Dependencies

~100–620KB
~12K SLoC