datetime.rs |
|
17282 |
de.rs |
Deserializing TOML into Rust structures.
This module contains all the Serde support for deserializing TOML documents
into Rust structures. Note that some top-level functions here are also
provided at the top of the crate. |
72977 |
lib.rs |
A [serde]-compatible [TOML]-parsing library
TOML itself is a simple, ergonomic, and readable configuration format:
```toml
[package]
name = "toml"
version = "0.4.2"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
[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 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),
}
```
TOML is similar to JSON with the notable addition of a [`Datetime`]
type. In general, TOML and JSON are interchangeable in terms of
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"));
```
The [`Value`] type implements a number of convenience methods and
traits; the example above uses [`FromStr`] to parse a [`str`] into a
[`Value`].
## 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 Value`
* `Serialize for Value`
* `Deserialize for Datetime`
* `Serialize for Datetime`
* `Deserializer for de::Deserializer`
* `Serializer for ser::Serializer`
* `Deserializer for Value`
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
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
use serde_derive::Deserialize;
#[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");
}
```
You can serialize types in a similar fashion:
```rust
use serde_derive::Serialize;
#[derive(Serialize)]
struct Config {
ip: String,
port: Option<u16>,
keys: Keys,
}
#[derive(Serialize)]
struct Keys {
github: String,
travis: Option<String>,
}
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()),
},
};
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/ |
4628 |
macros.rs |
|
20727 |
map.rs |
A map of String to toml::Value.
By default the map is backed by a [`BTreeMap`]. Enable the `preserve_order`
feature of toml-rs to use [`LinkedHashMap`] instead.
[`BTreeMap`]: https://doc.rust-lang.org/std/collections/struct.BTreeMap.html
[`LinkedHashMap`]: https://docs.rs/linked-hash-map/*/linked_hash_map/struct.LinkedHashMap.html |
16550 |
ser.rs |
Serializing Rust structures into TOML.
This module contains all the Serde support for serializing Rust structures
into TOML documents (as strings). Note that some top-level functions here
are also provided at the top of the crate.
Note that the TOML format has a restriction that if a table itself contains
tables, all keys with non-table values must be emitted first. This is
typically easy to ensure happens when you're defining a `struct` as you can
reorder the fields manually, but when working with maps (such as `BTreeMap`
or `HashMap`) this can lead to serialization errors. In those situations you
may use the `tables_last` function in this module like so:
```rust
# use serde_derive::Serialize;
# use std::collections::HashMap;
#[derive(Serialize)]
struct Manifest {
package: Package,
#[serde(serialize_with = "toml::ser::tables_last")]
dependencies: HashMap<String, Dependency>,
}
# type Package = String;
# type Dependency = String;
# fn main() {}
``` |
53694 |
spanned.rs |
|
4327 |
tokens.rs |
|
23744 |
value.rs |
Definition of a TOML value |
31018 |