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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::collections::BTreeMap;
use std::error;
use std::fmt;
use std::mem;
use {Value, Table};
#[cfg(feature = "rustc-serialize")] mod rustc_serialize;
#[cfg(feature = "serde")] mod serde;
pub struct Encoder {
pub toml: Table,
state: State,
}
#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub enum Error {
NeedsKey,
NoValue,
InvalidMapKeyLocation,
InvalidMapKeyType,
Custom(String),
}
#[derive(PartialEq)]
enum State {
Start,
NextKey(String),
NextArray(Vec<Value>),
NextMapKey,
}
impl Encoder {
pub fn new() -> Encoder {
Encoder { state: State::Start, toml: BTreeMap::new() }
}
fn emit_value(&mut self, v: Value) -> Result<(), Error> {
match mem::replace(&mut self.state, State::Start) {
State::NextKey(key) => { self.toml.insert(key, v); Ok(()) }
State::NextArray(mut vec) => {
vec.push(v);
self.state = State::NextArray(vec);
Ok(())
}
State::NextMapKey => {
match v {
Value::String(s) => { self.state = State::NextKey(s); Ok(()) }
_ => Err(Error::InvalidMapKeyType)
}
}
_ => Err(Error::NeedsKey)
}
}
fn emit_none(&mut self) -> Result<(), Error> {
match mem::replace(&mut self.state, State::Start) {
State::Start => unreachable!(),
State::NextKey(_) => Ok(()),
State::NextArray(..) => panic!("how to encode None in an array?"),
State::NextMapKey => Err(Error::InvalidMapKeyLocation),
}
}
fn seq<F>(&mut self, f: F) -> Result<(), Error>
where F: FnOnce(&mut Encoder) -> Result<(), Error>
{
let old = mem::replace(&mut self.state, State::NextArray(Vec::new()));
try!(f(self));
match mem::replace(&mut self.state, old) {
State::NextArray(v) => self.emit_value(Value::Array(v)),
_ => unreachable!(),
}
}
fn table<F>(&mut self, f: F) -> Result<(), Error>
where F: FnOnce(&mut Encoder) -> Result<(), Error>
{
match mem::replace(&mut self.state, State::Start) {
State::NextKey(key) => {
let mut nested = Encoder::new();
try!(f(&mut nested));
self.toml.insert(key, Value::Table(nested.toml));
Ok(())
}
State::NextArray(mut arr) => {
let mut nested = Encoder::new();
try!(f(&mut nested));
arr.push(Value::Table(nested.toml));
self.state = State::NextArray(arr);
Ok(())
}
State::Start => f(self),
State::NextMapKey => Err(Error::InvalidMapKeyLocation),
}
}
fn table_key<F>(&mut self, f: F) -> Result<(), Error>
where F: FnOnce(&mut Encoder) -> Result<(), Error>
{
match mem::replace(&mut self.state, State::NextMapKey) {
State::Start => {}
_ => return Err(Error::InvalidMapKeyLocation),
}
try!(f(self));
match self.state {
State::NextKey(_) => Ok(()),
_ => Err(Error::InvalidMapKeyLocation),
}
}
}
#[cfg(feature = "rustc-serialize")]
pub fn encode<T: ::rustc_serialize::Encodable>(t: &T) -> Value {
let mut e = Encoder::new();
t.encode(&mut e).unwrap();
Value::Table(e.toml)
}
#[cfg(all(not(feature = "rustc-serialize"), feature = "serde"))]
pub fn encode<T: ::serde::Serialize>(t: &T) -> Value {
let mut e = Encoder::new();
t.serialize(&mut e).unwrap();
Value::Table(e.toml)
}
#[cfg(feature = "rustc-serialize")]
pub fn encode_str<T: ::rustc_serialize::Encodable>(t: &T) -> String {
encode(t).to_string()
}
#[cfg(all(not(feature = "rustc-serialize"), feature = "serde"))]
pub fn encode_str<T: ::serde::Serialize>(t: &T) -> String {
encode(t).to_string()
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::NeedsKey => write!(f, "need a key to encode"),
Error::NoValue => write!(f, "no value to emit for a previous key"),
Error::InvalidMapKeyLocation => write!(f, "a map cannot be emitted \
at this location"),
Error::InvalidMapKeyType => write!(f, "only strings can be used as \
key types"),
Error::Custom(ref s) => write!(f, "custom error: {}", s),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str { "TOML encoding error" }
}