1 #![cfg(feature = "ini")]
2 
3 use serde_derive::Deserialize;
4 
5 use std::path::PathBuf;
6 
7 use config::{Config, File, FileFormat};
8 
9 #[derive(Debug, Deserialize, PartialEq)]
10 struct Place {
11     name: String,
12     longitude: f64,
13     latitude: f64,
14     favorite: bool,
15     reviews: u64,
16     rating: Option<f32>,
17 }
18 
19 #[derive(Debug, Deserialize, PartialEq)]
20 struct Settings {
21     debug: f64,
22     place: Place,
23 }
24 
make() -> Config25 fn make() -> Config {
26     Config::builder()
27         .add_source(File::new("tests/Settings", FileFormat::Ini))
28         .build()
29         .unwrap()
30 }
31 
32 #[test]
test_file()33 fn test_file() {
34     let c = make();
35     let s: Settings = c.try_deserialize().unwrap();
36     assert_eq!(
37         s,
38         Settings {
39             debug: 1.0,
40             place: Place {
41                 name: String::from("Torre di Pisa"),
42                 longitude: 43.722_498_5,
43                 latitude: 10.397_052_2,
44                 favorite: false,
45                 reviews: 3866,
46                 rating: Some(4.5),
47             },
48         }
49     );
50 }
51 
52 #[test]
test_error_parse()53 fn test_error_parse() {
54     let res = Config::builder()
55         .add_source(File::new("tests/Settings-invalid", FileFormat::Ini))
56         .build();
57 
58     let path: PathBuf = ["tests", "Settings-invalid.ini"].iter().collect();
59 
60     assert!(res.is_err());
61     assert_eq!(
62         res.unwrap_err().to_string(),
63         format!(
64             r#"2:0 expecting "[Some('='), Some(':')]" but found EOF. in {}"#,
65             path.display()
66         )
67     );
68 }
69