Skip to content

Commit cc0530a

Browse files
author
David Orchard
committed
Move order preservation under a feature gate
1 parent 622efac commit cc0530a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+358
-277
lines changed

Cargo.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ yaml = ["yaml-rust"]
2121
hjson = ["serde-hjson"]
2222
ini = ["rust-ini"]
2323
json5 = ["json5_rs"]
24+
preserve_order = ["linked-hash-map", "toml/preserve_order", "serde_json/preserve_order", "ron/indexmap"]
2425

2526
[dependencies]
2627
async-trait = "0.1.50"
2728
lazy_static = "1.0"
2829
serde = "1.0.8"
2930
nom = "6"
3031

31-
toml = { version = "0.5", features = ["preserve_order"], optional = true }
32-
serde_json = { version = "1.0.2", features = ["std", "preserve_order"], optional = true }
32+
toml = { version = "0.5", optional = true }
33+
serde_json = { version = "1.0.2", optional = true }
3334
yaml-rust = { version = "0.4", optional = true }
3435
serde-hjson = { version = "0.9", default-features = false, optional = true }
3536
rust-ini = { version = "0.17", optional = true }
36-
ron = { version = "0.6", features = ["indexmap"], optional = true }
37+
ron = { version = "0.6", optional = true }
3738
json5_rs = { version = "0.3", optional = true, package = "json5" }
38-
linked-hash-map = { version = "0.5.4", features = ["serde_impl"] }
39+
linked-hash-map = { version = "0.5.4", optional = true, features = ["serde_impl"] }
3940

4041
[dev-dependencies]
4142
serde_derive = "1.0.8"
@@ -45,3 +46,6 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util
4546
warp = "0.3.1"
4647
futures = "0.3.15"
4748
reqwest = "0.11.3"
49+
# Workaround to activate non-default features for tests:
50+
# https://github.com/rust-lang/cargo/issues/2911
51+
config = { path = ".", features = ["preserve_order"] }

examples/async_source/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use linked_hash_map::LinkedHashMap;
21
use std::error::Error;
32

4-
use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat};
3+
use config::{builder::AsyncState, AsyncSource, ConfigBuilder, ConfigError, FileFormat, MapImpl};
54

65
use async_trait::async_trait;
76
use futures::{select, FutureExt};
@@ -57,7 +56,7 @@ struct HttpSource {
5756

5857
#[async_trait]
5958
impl AsyncSource for HttpSource {
60-
async fn collect(&self) -> Result<LinkedHashMap<String, config::Value>, ConfigError> {
59+
async fn collect(&self) -> Result<MapImpl<String, config::Value>, ConfigError> {
6160
reqwest::get(&self.uri)
6261
.await
6362
.map_err(|e| ConfigError::Foreign(Box::new(e)))? // error conversion is possible from custom AsyncSource impls

examples/glob/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::path::Path;
2-
use std::collections::LinkedHashMap;
2+
use std::collections::MapImpl;
33
use config::*;
44
use glob::glob;
55

@@ -14,9 +14,9 @@ fn main() {
1414
.merge(File::from(Path::new("conf/05-some.yml"))).unwrap()
1515
.merge(File::from(Path::new("conf/99-extra.json"))).unwrap();
1616

17-
// Print out our settings (as a LinkedHashMap)
17+
// Print out our settings (as a MapImpl)
1818
println!("\n{:?} \n\n-----------",
19-
settings.try_into::<LinkedHashMap<String, String>>().unwrap());
19+
settings.try_into::<MapImpl<String, String>>().unwrap());
2020

2121
// Option 2
2222
// --------
@@ -28,9 +28,9 @@ fn main() {
2828
File::from(Path::new("conf/99-extra.json"))])
2929
.unwrap();
3030

31-
// Print out our settings (as a LinkedHashMap)
31+
// Print out our settings (as a MapImpl)
3232
println!("\n{:?} \n\n-----------",
33-
settings.try_into::<LinkedHashMap<String, String>>().unwrap());
33+
settings.try_into::<MapImpl<String, String>>().unwrap());
3434

3535
// Option 3
3636
// --------
@@ -43,7 +43,7 @@ fn main() {
4343
.collect::<Vec<_>>())
4444
.unwrap();
4545

46-
// Print out our settings (as a LinkedHashMap)
46+
// Print out our settings (as a MapImpl)
4747
println!("\n{:?} \n\n-----------",
48-
settings.try_into::<LinkedHashMap<String, String>>().unwrap());
48+
settings.try_into::<MapImpl<String, String>>().unwrap());
4949
}

examples/simple/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::LinkedHashMap;
1+
use std::collections::MapImpl;
22

33
fn main() {
44
let mut settings = config::Config::default();
@@ -9,7 +9,7 @@ fn main() {
99
// Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key
1010
.merge(config::Environment::with_prefix("APP")).unwrap();
1111

12-
// Print out our settings (as a LinkedHashMap)
12+
// Print out our settings (as a MapImpl)
1313
println!("{:?}",
14-
settings.try_into::<LinkedHashMap<String, String>>().unwrap());
14+
settings.try_into::<MapImpl<String, String>>().unwrap());
1515
}

examples/watch/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use config::*;
2-
use std::collections::LinkedHashMap;
2+
use std::collections::MapImpl;
33
use std::sync::RwLock;
44
use notify::{RecommendedWatcher, DebouncedEvent, Watcher, RecursiveMode};
55
use std::sync::mpsc::channel;
@@ -20,7 +20,7 @@ fn show() {
2020
.read()
2121
.unwrap()
2222
.clone()
23-
.try_into::<LinkedHashMap<String, String>>()
23+
.try_into::<MapImpl<String, String>>()
2424
.unwrap());
2525
}
2626

src/builder.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use linked_hash_map::LinkedHashMap;
21
use std::iter::IntoIterator;
32
use std::str::FromStr;
43

4+
55
use crate::error::Result;
6+
use crate::map::MapImpl;
67
use crate::source::AsyncSource;
78
use crate::{config::Config, path::Expression, source::Source, value::Value};
89

@@ -88,8 +89,8 @@ use crate::{config::Config, path::Expression, source::Source, value::Value};
8889
/// ```
8990
#[derive(Debug, Clone, Default)]
9091
pub struct ConfigBuilder<St: BuilderState> {
91-
defaults: LinkedHashMap<Expression, Value>,
92-
overrides: LinkedHashMap<Expression, Value>,
92+
defaults: MapImpl<Expression, Value>,
93+
overrides: MapImpl<Expression, Value>,
9394
state: St,
9495
}
9596

@@ -121,8 +122,8 @@ pub struct DefaultState {
121122
/// Refer to [`ConfigBuilder`] for similar API sample usage or to the examples folder of the crate, where such a source is implemented.
122123
#[derive(Debug, Clone, Default)]
123124
pub struct AsyncConfigBuilder {
124-
defaults: LinkedHashMap<Expression, Value>,
125-
overrides: LinkedHashMap<Expression, Value>,
125+
defaults: MapImpl<Expression, Value>,
126+
overrides: MapImpl<Expression, Value>,
126127
sources: Vec<SourceType>,
127128
}
128129

@@ -245,11 +246,11 @@ impl ConfigBuilder<DefaultState> {
245246
}
246247

247248
fn build_internal(
248-
defaults: LinkedHashMap<Expression, Value>,
249-
overrides: LinkedHashMap<Expression, Value>,
249+
defaults: MapImpl<Expression, Value>,
250+
overrides: MapImpl<Expression, Value>,
250251
sources: &[Box<dyn Source + Send + Sync>],
251252
) -> Result<Config> {
252-
let mut cache: Value = LinkedHashMap::<String, Value>::new().into();
253+
let mut cache: Value = MapImpl::<String, Value>::new().into();
253254

254255
// Add defaults
255256
for (key, val) in defaults.into_iter() {
@@ -323,11 +324,11 @@ impl ConfigBuilder<AsyncState> {
323324
}
324325

325326
async fn build_internal(
326-
defaults: LinkedHashMap<Expression, Value>,
327-
overrides: LinkedHashMap<Expression, Value>,
327+
defaults: MapImpl<Expression, Value>,
328+
overrides: MapImpl<Expression, Value>,
328329
sources: &[SourceType],
329330
) -> Result<Config> {
330-
let mut cache: Value = LinkedHashMap::<String, Value>::new().into();
331+
let mut cache: Value = MapImpl::<String, Value>::new().into();
331332

332333
// Add defaults
333334
for (key, val) in defaults.into_iter() {

src/config.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use linked_hash_map::LinkedHashMap;
21
use std::fmt::Debug;
32

43
use crate::builder::{ConfigBuilder, DefaultState};
54
use serde::de::Deserialize;
65
use serde::ser::Serialize;
76

87
use crate::error::*;
8+
use crate::map::MapImpl;
99
use crate::path;
1010
use crate::ser::ConfigSerializer;
1111
use crate::source::Source;
@@ -16,8 +16,8 @@ use crate::value::{Table, Value};
1616
/// them according to the source's priority.
1717
#[derive(Clone, Debug)]
1818
pub struct Config {
19-
defaults: LinkedHashMap<path::Expression, Value>,
20-
overrides: LinkedHashMap<path::Expression, Value>,
19+
defaults: MapImpl<path::Expression, Value>,
20+
overrides: MapImpl<path::Expression, Value>,
2121
sources: Vec<Box<dyn Source + Send + Sync>>,
2222

2323
/// Root of the cached configuration.
@@ -83,7 +83,7 @@ impl Config {
8383
#[deprecated(since = "0.12.0", note = "please use 'ConfigBuilder' instead")]
8484
pub fn refresh(&mut self) -> Result<&mut Config> {
8585
self.cache = {
86-
let mut cache: Value = LinkedHashMap::<String, Value>::new().into();
86+
let mut cache: Value = MapImpl::<String, Value>::new().into();
8787

8888
// Add defaults
8989
for (key, val) in self.defaults.iter() {
@@ -181,7 +181,7 @@ impl Config {
181181
self.get(key).and_then(Value::into_bool)
182182
}
183183

184-
pub fn get_table(&self, key: &str) -> Result<LinkedHashMap<String, Value>> {
184+
pub fn get_table(&self, key: &str) -> Result<MapImpl<String, Value>> {
185185
self.get(key).and_then(Value::into_table)
186186
}
187187

@@ -212,7 +212,7 @@ impl Source for Config {
212212
Box::new((*self).clone())
213213
}
214214

215-
fn collect(&self) -> Result<LinkedHashMap<String, Value>> {
215+
fn collect(&self) -> Result<MapImpl<String, Value>> {
216216
self.cache.clone().into_table()
217217
}
218218
}

src/de.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use linked_hash_map::LinkedHashMap;
21
use std::collections::VecDeque;
32
use std::iter::Enumerate;
43

54
use serde::de;
65

76
use crate::config::Config;
87
use crate::error::*;
8+
use crate::map::MapImpl;
99
use crate::value::{Table, Value, ValueKind};
1010

1111
impl<'de> de::Deserializer<'de> for Value {
@@ -200,7 +200,7 @@ struct MapAccess {
200200
}
201201

202202
impl MapAccess {
203-
fn new(table: LinkedHashMap<String, Value>) -> Self {
203+
fn new(table: MapImpl<String, Value>) -> Self {
204204
MapAccess {
205205
elements: table.into_iter().collect(),
206206
}

src/env.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use linked_hash_map::LinkedHashMap;
21
use std::env;
32

43
use crate::error::*;
4+
use crate::map::MapImpl;
55
use crate::source::Source;
66
use crate::value::{Value, ValueKind};
77

@@ -79,8 +79,8 @@ impl Source for Environment {
7979
Box::new((*self).clone())
8080
}
8181

82-
fn collect(&self) -> Result<LinkedHashMap<String, Value>> {
83-
let mut m = LinkedHashMap::new();
82+
fn collect(&self) -> Result<MapImpl<String, Value>> {
83+
let mut m = MapImpl::new();
8484
let uri: String = "the environment".into();
8585

8686
let separator = self.separator.as_deref().unwrap_or("");

src/file/format/hjson.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use linked_hash_map::LinkedHashMap;
21
use std::error::Error;
32

3+
use crate::map::MapImpl;
44
use crate::value::{Value, ValueKind};
55

66
pub fn parse(
77
uri: Option<&String>,
88
text: &str,
9-
) -> Result<LinkedHashMap<String, Value>, Box<dyn Error + Send + Sync>> {
9+
) -> Result<MapImpl<String, Value>, Box<dyn Error + Send + Sync>> {
1010
// Parse a JSON object value from the text
1111
// TODO: Have a proper error fire if the root of a file is ever not a Table
1212
let value = from_hjson_value(uri, &serde_hjson::from_str(text)?);
1313
match value.kind {
1414
ValueKind::Table(map) => Ok(map),
1515

16-
_ => Ok(LinkedHashMap::new()),
16+
_ => Ok(MapImpl::new()),
1717
}
1818
}
1919

@@ -30,7 +30,7 @@ fn from_hjson_value(uri: Option<&String>, value: &serde_hjson::Value) -> Value {
3030
serde_hjson::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)),
3131

3232
serde_hjson::Value::Object(ref table) => {
33-
let mut m = LinkedHashMap::new();
33+
let mut m = MapImpl::new();
3434

3535
for (key, value) in table {
3636
m.insert(key.clone(), from_hjson_value(uri, value));

0 commit comments

Comments
 (0)