1#[macro_use]
69extern crate log;
70extern crate toml;
71extern crate shareable;
72
73#[macro_use]
74mod test_macros;
75
76mod data;
77mod value;
78
79mod issues;
80mod item_def;
81mod item_value;
82mod enums;
83mod notify;
84mod toml_builder;
85mod toml_data;
86mod toml_def;
87mod toml_iter;
88
89pub use toml_data::TomlData;
90pub use toml_def::TomlDef;
91pub use data::{ItemDuration, ItemStr, ItemU16, ItemUsize};
92pub use issues::Issues;
93pub use enums::{Error, FormatDesc, Warning, ValidationError};
94
95#[cfg(test)]
96mod tests {
97 use super::{TomlDef, ItemDuration, ItemStr, ItemU16, ItemUsize};
98
99 #[test]
102 fn simple() {
103 let file = r#"
104 delay = "3d 4h 5m 6s 7ms"
105 name = "foo"
106 short = 16
107 large = 123456789
108 "#;
109
110 let data = TomlDef::new()
111 .add(ItemDuration::with_name("delay").min("6h").max("7d"))
112 .add(ItemStr::with_name("name").min(1).max(16))
113 .add(ItemU16::with_name("short").min(6).max(41))
114 .add(ItemUsize::with_name("large").min(3))
115 .parse_toml(file).unwrap();
116
117 assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
118 assert_eq!(data.get_str("name"), "foo");
119 assert_eq!(data.get_u16("short"), 16);
120 assert_eq!(data.get_usize("large"), 123456789);
121 }
122
123 #[test]
126 fn complex() {
127 let file = r#"
128 delay = "3d 4h 5m 6s 7ms"
129 name = "foo"
130 short = 16
131 large = 123456789
132
133 [dup]
134 delay = "7d 6h 5m 4s 3ms"
135 name = "bar"
136 short = 89
137 large = 987654321
138
139 [other]
140 test = "testing"
141 "#;
142
143 let data = TomlDef::new()
144 .add(ItemDuration::with_name("delay").min("6h").max("7d"))
145 .add(ItemStr::with_name("name").min(1).max(16))
146 .add(ItemU16::with_name("short").min(6).max(41))
147 .add(ItemUsize::with_name("large").max(200000000))
148 .add(TomlDef::with_name("dup")
149 .add(ItemDuration::with_name("delay").min("4d").max("8d"))
150 .add(ItemStr::with_name("name").min(1).max(16))
151 .add(ItemU16::with_name("short").min(22).max(100))
152 .add(ItemUsize::with_name("large").min(300000000)))
153 .add(TomlDef::with_name("other")
154 .add(ItemStr::with_name("test").min(1).max(16)))
155 .parse_toml(file).unwrap();
156
157 assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
158 assert_eq!(data.get_str("name"), "foo");
159 assert_eq!(data.get_u16("short"), 16);
160 assert_eq!(data.get_usize("large"), 123456789);
161
162 assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
163 assert_eq!(data.get_str("dup.name"), "bar");
164 assert_eq!(data.get_u16("dup.short"), 89);
165 assert_eq!(data.get_usize("dup.large"), 987654321);
166
167 assert_eq!(data.get_str("other.test"), "testing");
168 }
169
170 #[test]
173 fn optional_missing() {
174 let file = r#"
175 delay = "3d 4h 5m 6s 7ms"
176 short = 16
177 large = 123456789
178
179 [dup]
180 delay = "7d 6h 5m 4s 3ms"
181 name = "bar"
182 short = 89
183 "#;
184
185 let data = TomlDef::new()
186 .add(ItemDuration::with_name("delay").min("6h").max("7d"))
187 .add(ItemStr::with_name("name").min(1).max(16).optional())
188 .add(ItemU16::with_name("short").min(6).max(41))
189 .add(ItemUsize::with_name("large").max(200000000))
190 .add(TomlDef::with_name("dup")
191 .add(ItemDuration::with_name("delay").min("4d").max("8d"))
192 .add(ItemStr::with_name("name").min(1).max(16))
193 .add(ItemU16::with_name("short").min(22).max(100))
194 .add(ItemUsize::with_name("large").min(300000000).optional()))
195 .add(TomlDef::with_name("other").optional()
196 .add(ItemStr::with_name("test").min(1).max(16)))
197 .parse_toml(file).unwrap();
198
199 assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
200 assert !(data.optional_str("name").is_none());
201 assert_eq!(data.get_u16("short"), 16);
202 assert_eq!(data.get_usize("large"), 123456789);
203
204 assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
205 assert_eq!(data.get_str("dup.name"), "bar");
206 assert_eq!(data.get_u16("dup.short"), 89);
207 assert !(data.optional_usize("dup.large").is_none());
208
209 assert !(data.optional_str("other.test").is_none());
210 }
211
212 #[test]
215 fn optional_exist() {
216 let file = r#"
217 delay = "3d 4h 5m 6s 7ms"
218 name = "foo"
219 short = 16
220 large = 123456789
221
222 [dup]
223 delay = "7d 6h 5m 4s 3ms"
224 name = "bar"
225 short = 89
226 large = 987654321
227
228 [other]
229 test = "testing"
230 "#;
231
232 let data = TomlDef::new()
233 .add(ItemDuration::with_name("delay").min("6h").max("7d"))
234 .add(ItemStr::with_name("name").min(1).max(16).optional())
235 .add(ItemU16::with_name("short").min(6).max(41))
236 .add(ItemUsize::with_name("large").max(200000000))
237 .add(TomlDef::with_name("dup")
238 .add(ItemDuration::with_name("delay").min("4d").max("8d"))
239 .add(ItemStr::with_name("name").min(1).max(16))
240 .add(ItemU16::with_name("short").min(22).max(100))
241 .add(ItemUsize::with_name("large").min(300000000).optional()))
242 .add(TomlDef::with_name("other").optional()
243 .add(ItemStr::with_name("test").min(1).max(16)))
244 .parse_toml(file).unwrap();
245
246 assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
247 assert_eq!(data.optional_str("name").unwrap(), "foo");
248 assert_eq!(data.get_u16("short"), 16);
249 assert_eq!(data.get_usize("large"), 123456789);
250
251 assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
252 assert_eq!(data.get_str("dup.name"), "bar");
253 assert_eq!(data.get_u16("dup.short"), 89);
254 assert_eq!(data.optional_usize("dup.large").unwrap(), 987654321);
255
256 assert_eq!(data.optional_str("other.test").unwrap(), "testing");
257 }
258
259 #[test]
262 fn default() {
263 let file = r#"
264 delay = "3d 4h 5m 6s 7ms"
265 short = 16
266 large = 123456789
267
268 [dup]
269 delay = "7d 6h 5m 4s 3ms"
270 name = "bar"
271 short = 89
272 "#;
273
274 let data = TomlDef::new()
275 .add(ItemDuration::with_name("delay").min("6h").max("7d"))
276 .add(ItemStr::with_name("name").min(1).max(16).default("foo"))
277 .add(ItemU16::with_name("short").min(6).max(41))
278 .add(ItemUsize::with_name("large").max(200000000))
279 .add(TomlDef::with_name("dup")
280 .add(ItemDuration::with_name("delay").min("4d").max("8d"))
281 .add(ItemStr::with_name("name").min(1).max(16))
282 .add(ItemU16::with_name("short").min(22).max(100))
283 .add(ItemUsize::with_name("large").min(300000000).default(987654321)))
284 .add(TomlDef::with_name("other").optional()
285 .add(ItemStr::with_name("test").min(1).max(16).default("testing")))
286 .parse_toml(file).unwrap();
287
288 assert_eq!(data.get_duration("delay"), 3 * 24 * 60 * 60 * 1000 + 4 * 60 * 60 * 1000 + 5 * 60 * 1000 + 6 * 1000 + 7);
289 assert_eq!(data.get_str("name"), "foo");
290 assert_eq!(data.get_u16("short"), 16);
291 assert_eq!(data.get_usize("large"), 123456789);
292
293 assert_eq!(data.get_duration("dup.delay"), 7 * 24 * 60 * 60 * 1000 + 6 * 60 * 60 * 1000 + 5 * 60 * 1000 + 4 * 1000 + 3);
294 assert_eq!(data.get_str("dup.name"), "bar");
295 assert_eq!(data.get_u16("dup.short"), 89);
296 assert_eq!(data.get_usize("dup.large"), 987654321);
297
298 assert!(data.optional_str("other.test").is_none());
299 }
300}