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