1 //! A [TOML]-parsing library
3 //! This library implements a [TOML] v0.5.0 compatible parser,
4 //! primarily supporting the [`serde`] library for encoding/decoding
5 //! various types in Rust.
7 //! TOML itself is a simple, ergonomic, and readable configuration format:
13 //! authors = ["Alex Crichton <alex@alexcrichton.com>"]
19 //! The TOML format tends to be relatively common throughout the Rust community
20 //! for configuration, notably being used by [Cargo], Rust's package manager.
24 //! A value in TOML is represented with the [`Value`] enum in this crate:
32 //! Datetime(Datetime),
38 //! TOML is similar to JSON with the notable addition of a [`Datetime`]
39 //! type. In general, TOML and JSON are interchangeable in terms of
44 //! The easiest way to parse a TOML document is via the [`Value`] type:
49 //! let value = "foo = 'bar'".parse::<Value>().unwrap();
51 //! assert_eq!(value["foo"].as_str(), Some("bar"));
54 //! The [`Value`] type implements a number of convenience methods and
55 //! traits; the example above uses [`FromStr`] to parse a [`str`] into a
58 //! ## Deserialization and Serialization
60 //! This crate supports [`serde`] 1.0 with a number of
61 //! implementations of the `Deserialize`, `Serialize`, `Deserializer`, and
62 //! `Serializer` traits. Namely, you'll find:
64 //! * `Deserialize for Value`
65 //! * `Serialize for Value`
66 //! * `Deserialize for Datetime`
67 //! * `Serialize for Datetime`
68 //! * `Deserializer for de::Deserializer`
69 //! * `Serializer for ser::Serializer`
70 //! * `Deserializer for Value`
72 //! This means that you can use Serde to deserialize/serialize the
73 //! [`Value`] type as well as the [`Datetime`] type in this crate. You can also
74 //! use the [`Deserializer`], [`Serializer`], or [`Value`] type itself to act as
75 //! a deserializer/serializer for arbitrary types.
77 //! An example of deserializing with TOML is:
80 //! use serde_derive::Deserialize;
82 //! #[derive(Deserialize)]
85 //! port: Option<u16>,
89 //! #[derive(Deserialize)]
92 //! travis: Option<String>,
96 //! let config: Config = toml::from_str(r#"
100 //! github = 'xxxxxxxxxxxxxxxxx'
101 //! travis = 'yyyyyyyyyyyyyyyyy'
104 //! assert_eq!(config.ip, "127.0.0.1");
105 //! assert_eq!(config.port, None);
106 //! assert_eq!(config.keys.github, "xxxxxxxxxxxxxxxxx");
107 //! assert_eq!(config.keys.travis.as_ref().unwrap(), "yyyyyyyyyyyyyyyyy");
111 //! You can serialize types in a similar fashion:
114 //! use serde_derive::Serialize;
116 //! #[derive(Serialize)]
119 //! port: Option<u16>,
123 //! #[derive(Serialize)]
126 //! travis: Option<String>,
130 //! let config = Config {
131 //! ip: "127.0.0.1".to_string(),
134 //! github: "xxxxxxxxxxxxxxxxx".to_string(),
135 //! travis: Some("yyyyyyyyyyyyyyyyy".to_string()),
139 //! let toml = toml::to_string(&config).unwrap();
143 //! [TOML]: https://github.com/toml-lang/toml
144 //! [Cargo]: https://crates.io/
145 //! [`serde`]: https://serde.rs/
147 #![doc(html_root_url = "https://docs.rs/toml/0.5")]
148 #![deny(missing_docs)]
149 #![warn(rust_2018_idioms)]
150 // Makes rustc abort compilation if there are any unsafe blocks in the crate.
151 // Presence of this annotation is picked up by tools such as cargo-geiger
152 // and lets them ensure that there is indeed no unsafe code as opposed to
153 // something they couldn't detect (e.g. unsafe added via macro expansion, etc).
154 #![forbid(unsafe_code)]
159 pub use crate::value
::Value
;
164 pub use crate::ser
::{to_string, to_string_pretty, to_vec, Serializer}
;
167 pub use crate::de
::{from_slice, from_str, Deserializer}
;
175 pub use crate::spanned
::Spanned
;
178 #[allow(unused_imports)]
179 use crate::datetime
::Datetime
;
180 #[allow(unused_imports)]
181 use core
::str::FromStr
;