1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Mapping TOML documents onto your own types, with [serde].
//!
//! Available with the `serde` feature, which is off by default -- without it
//! the crate has no dependencies at all.
//!
//! ```toml
//! [dependencies]
//! tomlproc = { version = "0.1", features = ["serde"] }
//! ```
//!
//! ```
//! # #[cfg(feature = "serde")] fn main() {
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct Config {
//! name: String,
//! ports: Vec<u16>,
//! #[serde(default)]
//! verbose: bool,
//! }
//!
//! let config: Config = tomlproc::serde::from_str(r#"
//! name = "alpha"
//! ports = [8000, 8001]
//! "#).unwrap();
//!
//! assert_eq!(config, Config { name: "alpha".into(), ports: vec![8000, 8001], verbose: false });
//! assert_eq!(
//! tomlproc::serde::to_string(&config).unwrap(),
//! "name = \"alpha\"\nports = [8000, 8001]\nverbose = false\n",
//! );
//! # }
//! # #[cfg(not(feature = "serde"))] fn main() {}
//! ```
//!
//! # How TOML and serde line up
//!
//! - **`Option::None` is an absent key.** TOML has no null, so a `None` field
//! is left out of its table rather than written, and a missing key
//! deserializes back to `None`. A `None` with nowhere to be left out -- on
//! its own, or inside an array -- is an error.
//! - **Enums are externally tagged**, as in every self-describing format: a
//! variant with no payload is its bare name, and one with a payload is a
//! one-key table, `{ variant = payload }`.
//! - **Map keys are strings**, so a map keyed by a number, character, bool or
//! fieldless enum is written as text and read back out of it.
//! - **Date-times survive.** [`Datetime`](crate::Datetime) travels under a
//! private newtype that this module's [`Serializer`] and [`Deserializer`]
//! recognise, so a date-time stays a date-time; other formats see its text.
//! - **Strings are owned.** Deserialization reads out of a parsed
//! [`Value`], so a `&'de str` field cannot borrow from the input -- use
//! `String`.
//!
//! Errors from this module carry the [`key_path`](crate::Error::key_path) of
//! the value that would not fit:
//!
//! ```
//! # #[cfg(feature = "serde")] fn main() {
//! #[derive(serde::Deserialize, Debug)]
//! struct Config {
//! server: Server,
//! }
//! #[derive(serde::Deserialize, Debug)]
//! struct Server {
//! ports: Vec<u16>,
//! }
//!
//! let error = tomlproc::serde::from_str::<Config>("[server]\nports = [80, 'https']").unwrap_err();
//! assert_eq!(error.key_path().as_deref(), Some("server.ports.1"));
//! assert_eq!(
//! error.to_string(),
//! "TOML error at `server.ports.1`: invalid type: string \"https\", expected u16",
//! );
//! # }
//! # #[cfg(not(feature = "serde"))] fn main() {}
//! ```
//!
//! [serde]: https://serde.rs
use format;
use String;
use Serialize;
use DeserializeOwned;
pub use Deserializer;
pub use ;
use crateError;
use crateTable;
use crateValue;
/// Parses a TOML document into a type.
///
/// ```
/// # #[cfg(feature = "serde")] fn main() {
/// #[derive(serde::Deserialize)]
/// struct Config {
/// port: u16,
/// }
///
/// let config: Config = tomlproc::serde::from_str("port = 8080").unwrap();
/// assert_eq!(config.port, 8080);
/// # }
/// # #[cfg(not(feature = "serde"))] fn main() {}
/// ```
/// Parses a TOML document from bytes, which must be UTF-8, into a type.
/// Converts an already-parsed document into a type.
/// Converts an already-parsed value into a type.
///
/// ```
/// # #[cfg(feature = "serde")] fn main() {
/// let doc = tomlproc::parse("ports = [80, 443]").unwrap();
/// let ports: Vec<u16> = tomlproc::serde::from_value(doc["ports"].clone()).unwrap();
/// assert_eq!(ports, [80, 443]);
/// # }
/// # #[cfg(not(feature = "serde"))] fn main() {}
/// ```
/// Converts a value into a [`Value`].
///
/// ```
/// # #[cfg(feature = "serde")] fn main() {
/// let value = tomlproc::serde::to_value(&[1, 2, 3]).unwrap();
/// assert_eq!(value.to_string(), "[1, 2, 3]");
/// # }
/// # #[cfg(not(feature = "serde"))] fn main() {}
/// ```
Sized + Serialize>
/// Serializes a value as a TOML document.
///
/// The value has to become a table: a TOML document is a table, so a bare
/// integer or array has nowhere to go.
Sized + Serialize>
/// Serializes a value as a TOML document, laid out for a human to read.
///
/// The same document [`to_string`] writes, formatted as
/// [`crate::to_string_pretty`] does.
Sized + Serialize>
Sized + Serialize>