2017-01-29 18:53:20 -06:00
|
|
|
//! A [TOML]-parsing library
|
2014-06-20 19:30:08 -05:00
|
|
|
//!
|
2018-11-21 11:41:57 -06:00
|
|
|
//! This library implements a [TOML] v0.5.0 compatible parser,
|
2017-02-09 09:12:23 -06:00
|
|
|
//! primarily supporting the [`serde`] library for encoding/decoding
|
2017-01-29 18:53:20 -06:00
|
|
|
//! various types in Rust.
|
2014-06-20 19:30:08 -05:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! TOML itself is a simple, ergonomic, and readable configuration format:
|
2014-06-20 19:30:08 -05:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! ```toml
|
|
|
|
//! [package]
|
|
|
|
//! name = "toml"
|
2017-06-23 16:16:25 -05:00
|
|
|
//! version = "0.4.2"
|
2017-01-29 18:53:20 -06:00
|
|
|
//! authors = ["Alex Crichton <alex@alexcrichton.com>"]
|
|
|
|
//!
|
|
|
|
//! [dependencies]
|
2017-04-24 09:05:55 -05:00
|
|
|
//! serde = "1.0"
|
2017-01-29 18:53:20 -06:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! 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 value in TOML is represented with the `Value` enum in this crate:
|
|
|
|
//!
|
|
|
|
//! ```rust,ignore
|
|
|
|
//! pub enum Value {
|
|
|
|
//! String(String),
|
|
|
|
//! Integer(i64),
|
|
|
|
//! Float(f64),
|
|
|
|
//! Boolean(bool),
|
|
|
|
//! Datetime(Datetime),
|
|
|
|
//! Array(Array),
|
|
|
|
//! Table(Table),
|
|
|
|
//! }
|
2014-06-20 19:30:08 -05:00
|
|
|
//! ```
|
|
|
|
//!
|
2017-02-09 09:12:23 -06:00
|
|
|
//! TOML is similar to JSON with the notable addition of a `Datetime`
|
|
|
|
//! type. In general, TOML and JSON are interchangeable in terms of
|
2017-01-29 18:53:20 -06:00
|
|
|
//! formats.
|
|
|
|
//!
|
|
|
|
//! ## Parsing TOML
|
|
|
|
//!
|
|
|
|
//! The easiest way to parse a TOML document is via the `Value` type:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use toml::Value;
|
|
|
|
//!
|
|
|
|
//! let value = "foo = 'bar'".parse::<Value>().unwrap();
|
|
|
|
//!
|
|
|
|
//! assert_eq!(value["foo"].as_str(), Some("bar"));
|
2014-06-20 19:30:08 -05:00
|
|
|
//! ```
|
|
|
|
//!
|
2017-02-09 09:12:23 -06:00
|
|
|
//! The `Value` type implements a number of convenience methods and
|
|
|
|
//! traits; the example above uses `FromStr` to parse a `str` into a
|
|
|
|
//! `Value`.
|
2017-01-29 18:53:20 -06:00
|
|
|
//!
|
|
|
|
//! ## Deserialization and Serialization
|
|
|
|
//!
|
2017-05-02 11:19:40 -05:00
|
|
|
//! This crate supports [`serde`] 1.0 with a number of
|
2017-01-29 18:53:20 -06:00
|
|
|
//! implementations of the `Deserialize`, `Serialize`, `Deserializer`, and
|
2017-02-09 09:12:23 -06:00
|
|
|
//! `Serializer` traits. Namely, you'll find:
|
2017-01-29 18:53:20 -06:00
|
|
|
//!
|
|
|
|
//! * `Deserialize for Value`
|
|
|
|
//! * `Serialize for Value`
|
|
|
|
//! * `Deserialize for Datetime`
|
|
|
|
//! * `Serialize for Datetime`
|
|
|
|
//! * `Deserializer for de::Deserializer`
|
|
|
|
//! * `Serializer for ser::Serializer`
|
|
|
|
//! * `Deserializer for Value`
|
|
|
|
//!
|
2017-02-09 09:12:23 -06:00
|
|
|
//! This means that you can use Serde to deserialize/serialize the
|
|
|
|
//! `Value` type as well as the `Datetime` type in this crate. You can also
|
2017-01-29 18:53:20 -06:00
|
|
|
//! use the `Deserializer`, `Serializer`, or `Value` type itself to act as
|
|
|
|
//! a deserializer/serializer for arbitrary types.
|
|
|
|
//!
|
|
|
|
//! An example of deserializing with TOML is:
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2019-05-08 19:37:38 -05:00
|
|
|
//! use serde_derive::Deserialize;
|
2017-01-29 18:53:20 -06:00
|
|
|
//!
|
|
|
|
//! #[derive(Deserialize)]
|
|
|
|
//! struct Config {
|
|
|
|
//! ip: String,
|
|
|
|
//! port: Option<u16>,
|
|
|
|
//! keys: Keys,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[derive(Deserialize)]
|
|
|
|
//! struct Keys {
|
|
|
|
//! github: String,
|
|
|
|
//! travis: Option<String>,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! 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");
|
|
|
|
//! }
|
|
|
|
//! ```
|
2014-06-22 01:35:49 -05:00
|
|
|
//!
|
2017-02-09 09:12:23 -06:00
|
|
|
//! You can serialize types in a similar fashion:
|
2014-06-22 01:35:49 -05:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! ```rust
|
2019-05-08 19:37:38 -05:00
|
|
|
//! use serde_derive::Serialize;
|
2014-06-22 01:35:49 -05:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! #[derive(Serialize)]
|
|
|
|
//! struct Config {
|
|
|
|
//! ip: String,
|
|
|
|
//! port: Option<u16>,
|
|
|
|
//! keys: Keys,
|
|
|
|
//! }
|
2014-06-22 01:35:49 -05:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! #[derive(Serialize)]
|
|
|
|
//! struct Keys {
|
|
|
|
//! github: String,
|
|
|
|
//! travis: Option<String>,
|
|
|
|
//! }
|
2017-01-09 19:37:05 -06:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! fn main() {
|
|
|
|
//! let config = Config {
|
|
|
|
//! ip: "127.0.0.1".to_string(),
|
|
|
|
//! port: None,
|
|
|
|
//! keys: Keys {
|
|
|
|
//! github: "xxxxxxxxxxxxxxxxx".to_string(),
|
|
|
|
//! travis: Some("yyyyyyyyyyyyyyyyy".to_string()),
|
|
|
|
//! },
|
|
|
|
//! };
|
2017-01-09 19:37:05 -06:00
|
|
|
//!
|
2017-01-29 18:53:20 -06:00
|
|
|
//! let toml = toml::to_string(&config).unwrap();
|
|
|
|
//! }
|
|
|
|
//! ```
|
2017-01-09 19:37:05 -06:00
|
|
|
//!
|
2017-04-05 13:51:36 -05:00
|
|
|
//! [TOML]: https://github.com/toml-lang/toml
|
2017-01-29 18:53:20 -06:00
|
|
|
//! [Cargo]: https://crates.io/
|
|
|
|
//! [`serde`]: https://serde.rs/
|
2015-03-03 22:35:04 -06:00
|
|
|
|
2019-04-25 13:15:42 -05:00
|
|
|
#![doc(html_root_url = "https://docs.rs/toml/0.5")]
|
2014-10-31 10:27:24 -05:00
|
|
|
#![deny(missing_docs)]
|
2019-05-08 19:37:38 -05:00
|
|
|
#![warn(rust_2018_idioms)]
|
2019-09-30 09:57:16 -05:00
|
|
|
// Makes rustc abort compilation if there are any unsafe blocks in the crate.
|
|
|
|
// Presence of this annotation is picked up by tools such as cargo-geiger
|
|
|
|
// and lets them ensure that there is indeed no unsafe code as opposed to
|
|
|
|
// something they couldn't detect (e.g. unsafe added via macro expansion, etc).
|
|
|
|
#![forbid(unsafe_code)]
|
2017-01-29 18:53:20 -06:00
|
|
|
|
2018-05-14 09:59:24 -05:00
|
|
|
pub mod map;
|
2017-01-29 18:53:20 -06:00
|
|
|
pub mod value;
|
|
|
|
#[doc(no_inline)]
|
2019-05-08 14:12:14 -05:00
|
|
|
pub use crate::value::Value;
|
2017-04-05 14:30:32 -05:00
|
|
|
mod datetime;
|
2017-01-29 18:53:20 -06:00
|
|
|
|
|
|
|
pub mod ser;
|
|
|
|
#[doc(no_inline)]
|
2019-05-08 14:12:14 -05:00
|
|
|
pub use crate::ser::{to_string, to_string_pretty, to_vec, Serializer};
|
2017-01-29 18:53:20 -06:00
|
|
|
pub mod de;
|
|
|
|
#[doc(no_inline)]
|
2019-05-08 14:12:14 -05:00
|
|
|
pub use crate::de::{from_slice, from_str, Deserializer};
|
2017-01-29 18:53:20 -06:00
|
|
|
mod tokens;
|
2017-11-12 17:17:52 -06:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod macros;
|
2018-05-05 16:41:57 -05:00
|
|
|
|
2018-05-09 14:26:59 -05:00
|
|
|
mod spanned;
|
2019-05-08 14:12:14 -05:00
|
|
|
pub use crate::spanned::Spanned;
|