1 //! A [TOML]-parsing library
3 //! [TOML]: https://github.com/toml-lang/toml
5 //! This library implements a [TOML] v0.4.0 compatible parser,
6 //! primarily supporting the [`serde`] library for encoding/decoding
7 //! various types in Rust.
9 //! TOML itself is a simple, ergonomic, and readable configuration format:
15 //! authors = ["Alex Crichton <alex@alexcrichton.com>"]
21 //! The TOML format tends to be relatively common throughout the Rust community
22 //! for configuration, notably being used by [Cargo], Rust's package manager.
26 //! A value in TOML is represented with the `Value` enum in this crate:
34 //! Datetime(Datetime),
40 //! TOML is similar to JSON with the notable addition of a `Datetime`
41 //! type. In general, TOML and JSON are interchangeable in terms of
46 //! The easiest way to parse a TOML document is via the `Value` type:
51 //! let value = "foo = 'bar'".parse::<Value>().unwrap();
53 //! assert_eq!(value["foo"].as_str(), Some("bar"));
56 //! The `Value` type implements a number of convenience methods and
57 //! traits; the example above uses `FromStr` to parse a `str` into a
60 //! ## Deserialization and Serialization
62 //! This crate supports [`serde`] 0.9 with a number of
63 //! implementations of the `Deserialize`, `Serialize`, `Deserializer`, and
64 //! `Serializer` traits. Namely, you'll find:
66 //! * `Deserialize for Value`
67 //! * `Serialize for Value`
68 //! * `Deserialize for Datetime`
69 //! * `Serialize for Datetime`
70 //! * `Deserializer for de::Deserializer`
71 //! * `Serializer for ser::Serializer`
72 //! * `Deserializer for Value`
74 //! This means that you can use Serde to deserialize/serialize the
75 //! `Value` type as well as the `Datetime` type in this crate. You can also
76 //! use the `Deserializer`, `Serializer`, or `Value` type itself to act as
77 //! a deserializer/serializer for arbitrary types.
79 //! An example of deserializing with TOML is:
83 //! extern crate serde_derive;
84 //! extern crate toml;
86 //! #[derive(Deserialize)]
89 //! port: Option<u16>,
93 //! #[derive(Deserialize)]
96 //! travis: Option<String>,
100 //! let config: Config = toml::from_str(r#"
104 //! github = 'xxxxxxxxxxxxxxxxx'
105 //! travis = 'yyyyyyyyyyyyyyyyy'
108 //! assert_eq!(config.ip, "127.0.0.1");
109 //! assert_eq!(config.port, None);
110 //! assert_eq!(config.keys.github, "xxxxxxxxxxxxxxxxx");
111 //! assert_eq!(config.keys.travis.as_ref().unwrap(), "yyyyyyyyyyyyyyyyy");
115 //! You can serialize types in a similar fashion:
119 //! extern crate serde_derive;
120 //! extern crate toml;
122 //! #[derive(Serialize)]
125 //! port: Option<u16>,
129 //! #[derive(Serialize)]
132 //! travis: Option<String>,
136 //! let config = Config {
137 //! ip: "127.0.0.1".to_string(),
140 //! github: "xxxxxxxxxxxxxxxxx".to_string(),
141 //! travis: Some("yyyyyyyyyyyyyyyyy".to_string()),
145 //! let toml = toml::to_string(&config).unwrap();
149 //! [Cargo]: https://crates.io/
150 //! [`serde`]: https://serde.rs/
152 #![doc(html_root_url = "https://docs.rs/toml/0.3")]
153 #![deny(missing_docs)]
161 pub use value
::Value
;
165 pub use ser
::{to_string, to_vec, Serializer}
;
168 pub use de
::{from_slice, from_str, Deserializer}
;