1 #![cfg(feature = "toml")]
2
3 use serde_derive::Deserialize;
4
5 use std::path::PathBuf;
6
7 use config::{Config, File, FileFormat, Map, Value};
8 use float_cmp::ApproxEqUlps;
9
10 #[derive(Debug, Deserialize)]
11 struct Place {
12 number: PlaceNumber,
13 name: String,
14 longitude: f64,
15 latitude: f64,
16 favorite: bool,
17 telephone: Option<String>,
18 reviews: u64,
19 creator: Map<String, Value>,
20 rating: Option<f32>,
21 }
22
23 #[derive(Debug, Deserialize, PartialEq)]
24 struct PlaceNumber(u8);
25
26 #[derive(Debug, Deserialize, PartialEq)]
27 struct AsciiCode(i8);
28
29 #[derive(Debug, Deserialize)]
30 struct Settings {
31 debug: f64,
32 production: Option<String>,
33 code: AsciiCode,
34 place: Place,
35 #[serde(rename = "arr")]
36 elements: Vec<String>,
37 }
38
39 #[cfg(test)]
make() -> Config40 fn make() -> Config {
41 Config::builder()
42 .add_source(File::new("tests/Settings", FileFormat::Toml))
43 .build()
44 .unwrap()
45 }
46
47 #[test]
test_file()48 fn test_file() {
49 let c = make();
50
51 // Deserialize the entire file as single struct
52 let s: Settings = c.try_deserialize().unwrap();
53
54 assert!(s.debug.approx_eq_ulps(&1.0, 2));
55 assert_eq!(s.production, Some("false".to_string()));
56 assert_eq!(s.code, AsciiCode(53));
57 assert_eq!(s.place.number, PlaceNumber(1));
58 assert_eq!(s.place.name, "Torre di Pisa");
59 assert!(s.place.longitude.approx_eq_ulps(&43.722_498_5, 2));
60 assert!(s.place.latitude.approx_eq_ulps(&10.397_052_2, 2));
61 assert!(!s.place.favorite);
62 assert_eq!(s.place.reviews, 3866);
63 assert_eq!(s.place.rating, Some(4.5));
64 assert_eq!(s.place.telephone, None);
65 assert_eq!(s.elements.len(), 10);
66 assert_eq!(s.elements[3], "4".to_string());
67 if cfg!(feature = "preserve_order") {
68 assert_eq!(
69 s.place
70 .creator
71 .into_iter()
72 .collect::<Vec<(String, config::Value)>>(),
73 vec![
74 ("name".to_string(), "John Smith".into()),
75 ("username".into(), "jsmith".into()),
76 ("email".into(), "jsmith@localhost".into()),
77 ]
78 );
79 } else {
80 assert_eq!(
81 s.place.creator["name"].clone().into_string().unwrap(),
82 "John Smith".to_string()
83 );
84 }
85 }
86
87 #[test]
test_error_parse()88 fn test_error_parse() {
89 let res = Config::builder()
90 .add_source(File::new("tests/Settings-invalid", FileFormat::Toml))
91 .build();
92
93 let path_with_extension: PathBuf = ["tests", "Settings-invalid.toml"].iter().collect();
94
95 assert!(res.is_err());
96 assert_eq!(
97 res.unwrap_err().to_string(),
98 format!(
99 "invalid TOML value, did you mean to use a quoted string? at line 2 column 9 in {}",
100 path_with_extension.display()
101 )
102 );
103 }
104