Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: preserve_order feature #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ json = ["serde_json"]
yaml = ["serde_yaml"]
parse-value = ["pear"]
test = ["tempfile", "parking_lot"]
preserve_order = ["indexmap", "toml?/preserve_order", "serde_json?/preserve_order"]
# toml = ["toml"]

[dependencies]
Expand All @@ -28,6 +29,7 @@ serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.9", optional = true }
tempfile = { version = "3", optional = true }
parking_lot = { version = "0.12", optional = true }
indexmap = { version = "2.1.0", features = ["serde"], optional = true }

[target.'cfg(any(target_pointer_width = "8", target_pointer_width = "16", target_pointer_width = "32"))'.dependencies]
atomic = "0.6.0"
Expand Down
17 changes: 9 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,15 @@
//! To help with compilation times, types, modules, and providers are gated by
//! features. They are:
//!
//! | feature | gated namespace | description |
//! |---------|-----------------------------|-------------------------------------------|
//! | `test` | [`Jail`] | Semi-sandboxed environment for testing. |
//! | `env` | [`providers::Env`] | Environment variable [`Provider`]. |
//! | `toml` | [`providers::Toml`] | TOML file/string [`Provider`]. |
//! | `json` | [`providers::Json`] | JSON file/string [`Provider`]. |
//! | `yaml` | [`providers::Yaml`] | YAML file/string [`Provider`]. |
//! | `yaml` | [`providers::YamlExtended`] | [YAML Extended] file/string [`Provider`]. |
//! | feature | gated namespace | description |
//! |------------------|-----------------------------|----------------------------------------------------------------------------|
//! | `test` | [`Jail`] | Semi-sandboxed environment for testing. |
//! | `env` | [`providers::Env`] | Environment variable [`Provider`]. |
//! | `toml` | [`providers::Toml`] | TOML file/string [`Provider`]. |
//! | `json` | [`providers::Json`] | JSON file/string [`Provider`]. |
//! | `yaml` | [`providers::Yaml`] | YAML file/string [`Provider`]. |
//! | `yaml` | [`providers::YamlExtended`] | [YAML Extended] file/string [`Provider`]. |
//! | `preserve_order` | nil | Preserve map order by using [IndexMap](https://github.com/bluss/indexmap). |
//!
//! [YAML Extended]: providers::YamlExtended::from_str()
//!
Expand Down
3 changes: 3 additions & 0 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ impl<'de: 'c, 'c> Deserializer<'de> for ConfiguredValueDe<'c> {
}
}

#[cfg(not(feature = "preserve_order"))]
use std::collections::btree_map::Iter;
#[cfg(feature = "preserve_order")]
use indexmap::map::Iter;

pub struct MapDe<'m, D, F: Fn(&'m Value) -> D> {
iter: Iter<'m, String, Value>,
Expand Down
9 changes: 7 additions & 2 deletions src/value/value.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use std::str::Split;
use std::collections::BTreeMap;

use serde::Serialize;
#[cfg(not(feature = "preserve_order"))]
use std::collections::BTreeMap;

use crate::value::{Tag, ValueSerializer};
use crate::error::{Error, Actual};

/// An alias to the type of map used in [`Value::Dict`].
#[cfg(not(feature = "preserve_order"))]
pub type Map<K, V> = BTreeMap<K, V>;

/// An alias to the type of map used in [`Value::Dict`].
#[cfg(feature = "preserve_order")]
pub type Map<K, V> = indexmap::IndexMap<K, V>;
Comment on lines +10 to +15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot do this: features must be additive. This breaks that.


/// An alias to a [`Map`] from `String` to [`Value`]s.
pub type Dict = Map<String, Value>;

Expand Down