1 // Please note: This file is named "weird" keys because these things are normally not keys, not
2 // because your software is weird if it expects these keys in the config file.
3 //
4 // Please don't be offended!
5 //
6 
7 use serde_derive::{Deserialize, Serialize};
8 
9 use config::{File, FileFormat};
10 
11 /// Helper fn to test the different deserializations
test_config_as<'a, T>(config: &str, format: FileFormat) -> T where T: serde::Deserialize<'a> + std::fmt::Debug,12 fn test_config_as<'a, T>(config: &str, format: FileFormat) -> T
13 where
14     T: serde::Deserialize<'a> + std::fmt::Debug,
15 {
16     let cfg = config::Config::builder()
17         .add_source(File::from_str(config, format))
18         .build();
19 
20     assert!(cfg.is_ok(), "Config could not be built: {:?}", cfg);
21     let cfg = cfg.unwrap().try_deserialize();
22 
23     assert!(cfg.is_ok(), "Config could not be transformed: {:?}", cfg);
24     let cfg: T = cfg.unwrap();
25     cfg
26 }
27 
28 #[derive(Debug, Serialize, Deserialize)]
29 struct SettingsColon {
30     #[serde(rename = "foo:foo")]
31     foo: u8,
32 
33     bar: u8,
34 }
35 
36 #[test]
test_colon_key_toml()37 fn test_colon_key_toml() {
38     let config = r#"
39         "foo:foo" = 8
40         bar = 12
41     "#;
42 
43     let cfg = test_config_as::<SettingsColon>(config, FileFormat::Toml);
44     assert_eq!(cfg.foo, 8);
45     assert_eq!(cfg.bar, 12);
46 }
47 
48 #[test]
test_colon_key_json()49 fn test_colon_key_json() {
50     let config = r#" {"foo:foo": 8, "bar": 12 } "#;
51 
52     let cfg = test_config_as::<SettingsColon>(config, FileFormat::Json);
53     assert_eq!(cfg.foo, 8);
54     assert_eq!(cfg.bar, 12);
55 }
56 
57 #[derive(Debug, Serialize, Deserialize)]
58 struct SettingsSlash {
59     #[serde(rename = "foo/foo")]
60     foo: u8,
61     bar: u8,
62 }
63 
64 #[test]
test_slash_key_toml()65 fn test_slash_key_toml() {
66     let config = r#"
67         "foo/foo" = 8
68         bar = 12
69     "#;
70 
71     let cfg = test_config_as::<SettingsSlash>(config, FileFormat::Toml);
72     assert_eq!(cfg.foo, 8);
73     assert_eq!(cfg.bar, 12);
74 }
75 
76 #[test]
test_slash_key_json()77 fn test_slash_key_json() {
78     let config = r#" {"foo/foo": 8, "bar": 12 } "#;
79 
80     let cfg = test_config_as::<SettingsSlash>(config, FileFormat::Json);
81     assert_eq!(cfg.foo, 8);
82     assert_eq!(cfg.bar, 12);
83 }
84 
85 #[derive(Debug, Serialize, Deserialize)]
86 struct SettingsDoubleBackslash {
87     #[serde(rename = "foo\\foo")]
88     foo: u8,
89     bar: u8,
90 }
91 
92 #[test]
test_doublebackslash_key_toml()93 fn test_doublebackslash_key_toml() {
94     let config = r#"
95         "foo\\foo" = 8
96         bar = 12
97     "#;
98 
99     let cfg = test_config_as::<SettingsDoubleBackslash>(config, FileFormat::Toml);
100     assert_eq!(cfg.foo, 8);
101     assert_eq!(cfg.bar, 12);
102 }
103 
104 #[test]
test_doublebackslash_key_json()105 fn test_doublebackslash_key_json() {
106     let config = r#" {"foo\\foo": 8, "bar": 12 } "#;
107 
108     let cfg = test_config_as::<SettingsDoubleBackslash>(config, FileFormat::Json);
109     assert_eq!(cfg.foo, 8);
110     assert_eq!(cfg.bar, 12);
111 }
112