Skip to content

Commit 0d58da2

Browse files
Merge pull request #354 from YounessBird/fix-uppercase-lowercase-isses
Fix uppercase lowercase isses
2 parents bfe819a + b2dfb93 commit 0d58da2

23 files changed

+682
-18
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
[RON]: https://github.com/ron-rs/ron
2424
[JSON5]: https://github.com/callum-oakley/json5-rs
2525

26-
Please note that this library can not be used to write changed configuration
27-
values back to the configuration file(s)!
26+
Please note this library
27+
28+
- can not be used to write changed configuration values back to the configuration file(s)!
29+
- Is case insensitive and all the keys are converted to lowercase internally
2830

2931
## Usage
3032

@@ -63,4 +65,4 @@ We currently support Rust 1.56.1 and newer.
6365

6466
config-rs is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).
6567

66-
See LICENSE-APACHE and LICENSE-MIT for details.
68+
See LICENSE-APACHE and LICENSE-MIT for details.

src/config.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ impl Config {
110110
where
111111
T: Into<Value>,
112112
{
113-
self.defaults.insert(key.parse()?, value.into());
113+
self.defaults
114+
.insert(key.to_lowercase().as_str().parse()?, value.into());
114115

115116
#[allow(deprecated)]
116117
self.refresh()
@@ -129,15 +130,16 @@ impl Config {
129130
where
130131
T: Into<Value>,
131132
{
132-
self.overrides.insert(key.parse()?, value.into());
133+
self.overrides
134+
.insert(key.to_lowercase().as_str().parse()?, value.into());
133135

134136
#[allow(deprecated)]
135137
self.refresh()
136138
}
137139

138140
#[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")]
139141
pub fn set_once(&mut self, key: &str, value: Value) -> Result<()> {
140-
let expr: path::Expression = key.parse()?;
142+
let expr: path::Expression = key.to_lowercase().as_str().parse()?;
141143

142144
// Traverse the cache using the path to (possibly) retrieve a value
143145
if let Some(ref mut val) = expr.get_mut(&mut self.cache) {
@@ -149,6 +151,8 @@ impl Config {
149151
}
150152

151153
pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> {
154+
let k = key.to_lowercase();
155+
let key = k.as_str();
152156
// Parse the key into a path expression
153157
let expr: path::Expression = key.parse()?;
154158

src/de.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl EnumAccess {
272272
fn variant_deserializer(&self, name: &str) -> Result<StrDeserializer> {
273273
self.variants
274274
.iter()
275-
.find(|&&s| s == name)
275+
.find(|&&s| s.to_lowercase() == name.to_lowercase()) // changing to lowercase will enable deserialization of lowercase values to enums
276276
.map(|&s| StrDeserializer(s))
277277
.ok_or_else(|| self.no_constructor_error(name))
278278
}

src/path/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl Expression {
120120
match *self {
121121
Self::Identifier(ref id) => match root.kind {
122122
ValueKind::Table(ref mut map) => Some(
123-
map.entry(id.clone())
123+
map.entry(id.to_lowercase())
124124
.or_insert_with(|| Value::new(None, ValueKind::Nil)),
125125
),
126126

@@ -131,15 +131,15 @@ impl Expression {
131131
Some(value) => {
132132
if let ValueKind::Table(ref mut map) = value.kind {
133133
Some(
134-
map.entry(key.clone())
134+
map.entry(key.to_lowercase())
135135
.or_insert_with(|| Value::new(None, ValueKind::Nil)),
136136
)
137137
} else {
138138
*value = Map::<String, Value>::new().into();
139139

140140
if let ValueKind::Table(ref mut map) = value.kind {
141141
Some(
142-
map.entry(key.clone())
142+
map.entry(key.to_lowercase())
143143
.or_insert_with(|| Value::new(None, ValueKind::Nil)),
144144
)
145145
} else {
@@ -194,25 +194,25 @@ impl Expression {
194194
ValueKind::Table(ref incoming_map) => {
195195
// Pull out another table
196196
let target = if let ValueKind::Table(ref mut map) = root.kind {
197-
map.entry(id.clone())
197+
map.entry(id.to_lowercase())
198198
.or_insert_with(|| Map::<String, Value>::new().into())
199199
} else {
200200
unreachable!();
201201
};
202202

203203
// Continue the deep merge
204204
for (key, val) in incoming_map {
205-
Self::Identifier(key.clone()).set(target, val.clone());
205+
Self::Identifier(key.to_lowercase()).set(target, val.clone());
206206
}
207207
}
208208

209209
_ => {
210210
if let ValueKind::Table(ref mut map) = root.kind {
211211
// Just do a simple set
212-
if let Some(existing) = map.get_mut(id) {
212+
if let Some(existing) = map.get_mut(&id.to_lowercase()) {
213213
*existing = value;
214214
} else {
215-
map.insert(id.clone(), value);
215+
map.insert(id.to_lowercase(), value);
216216
}
217217
}
218218
}
@@ -225,7 +225,7 @@ impl Expression {
225225
// Didn't find a table. Oh well. Make a table and do this anyway
226226
*parent = Map::<String, Value>::new().into();
227227
}
228-
Self::Identifier(key.clone()).set(parent, value);
228+
Self::Identifier(key.to_lowercase()).set(parent, value);
229229
}
230230
}
231231

tests/Settings-enum-test.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Test for Enum deserialization. See file_tomls.rs override tests
2+
bar = "bar is a lowercase param"

tests/Settings-enum-test.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bar": "bar is a lowercase param"
3+
}

tests/Settings-enum-test.json5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
bar: "bar is a lowercase param",
3+
}

tests/Settings-enum-test.ron

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(
2+
bar: "bar is a lowercase param"
3+
)

tests/Settings-enum-test.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Test for Enum deserialization. See file_tomls.rs override tests
2+
bar = "bar is a lowercase param"

tests/Settings-enum-test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Test for Enum deserialization. See file_yaml.rs override tests
2+
bar: bar is a lowercase param

0 commit comments

Comments
 (0)