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
|
// Please note: This file is named "weird" keys because these things are normally not keys, not
// because your software is weird if it expects these keys in the config file.
//
// Please don't be offended!
//
use serde_derive::{Deserialize, Serialize};
use config::{File, FileFormat};
/// Helper fn to test the different deserializations
fn test_config_as<'a, T>(config: &str, format: FileFormat) -> T
where
T: serde::Deserialize<'a> + std::fmt::Debug,
{
let cfg = config::Config::builder()
.add_source(File::from_str(config, format))
.build();
assert!(cfg.is_ok(), "Config could not be built: {:?}", cfg);
let cfg = cfg.unwrap().try_deserialize();
assert!(cfg.is_ok(), "Config could not be transformed: {:?}", cfg);
let cfg: T = cfg.unwrap();
cfg
}
#[derive(Debug, Serialize, Deserialize)]
struct SettingsColon {
#[serde(rename = "foo:foo")]
foo: u8,
bar: u8,
}
#[test]
fn test_colon_key_toml() {
let config = r#"
"foo:foo" = 8
bar = 12
"#;
let cfg = test_config_as::<SettingsColon>(config, FileFormat::Toml);
assert_eq!(cfg.foo, 8);
assert_eq!(cfg.bar, 12);
}
#[test]
fn test_colon_key_json() {
let config = r#" {"foo:foo": 8, "bar": 12 } "#;
let cfg = test_config_as::<SettingsColon>(config, FileFormat::Json);
assert_eq!(cfg.foo, 8);
assert_eq!(cfg.bar, 12);
}
#[derive(Debug, Serialize, Deserialize)]
struct SettingsSlash {
#[serde(rename = "foo/foo")]
foo: u8,
bar: u8,
}
#[test]
fn test_slash_key_toml() {
let config = r#"
"foo/foo" = 8
bar = 12
"#;
let cfg = test_config_as::<SettingsSlash>(config, FileFormat::Toml);
assert_eq!(cfg.foo, 8);
assert_eq!(cfg.bar, 12);
}
#[test]
fn test_slash_key_json() {
let config = r#" {"foo/foo": 8, "bar": 12 } "#;
let cfg = test_config_as::<SettingsSlash>(config, FileFormat::Json);
assert_eq!(cfg.foo, 8);
assert_eq!(cfg.bar, 12);
}
#[derive(Debug, Serialize, Deserialize)]
struct SettingsDoubleBackslash {
#[serde(rename = "foo\\foo")]
foo: u8,
bar: u8,
}
#[test]
fn test_doublebackslash_key_toml() {
let config = r#"
"foo\\foo" = 8
bar = 12
"#;
let cfg = test_config_as::<SettingsDoubleBackslash>(config, FileFormat::Toml);
assert_eq!(cfg.foo, 8);
assert_eq!(cfg.bar, 12);
}
#[test]
fn test_doublebackslash_key_json() {
let config = r#" {"foo\\foo": 8, "bar": 12 } "#;
let cfg = test_config_as::<SettingsDoubleBackslash>(config, FileFormat::Json);
assert_eq!(cfg.foo, 8);
assert_eq!(cfg.bar, 12);
}
|