Skip to content

Commit

Permalink
Merge pull request #25 from DeterminateSystems/use-indexmap
Browse files Browse the repository at this point in the history
Use indexmap
  • Loading branch information
Hoverbear authored Oct 2, 2023
2 parents 6048524 + 3d0351d commit 1dcd5d0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ repository = "https://github.com/DeterminateSystems/nix-config-parser"
documentation = "https://docs.rs/nix-config-parser/latest/nix_config_parser"

[features]
serde = ["dep:serde"]
serde = ["dep:serde", "indexmap/serde"]

[dependencies]
indexmap = "2"
serde = { version = "1.0.152", features = ["derive"], optional = true }
thiserror = "1.0.38"

Expand Down
53 changes: 43 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! # nix-config-parser
//!
//! A simple parser for the Nix configuration file format.
use std::{
collections::HashMap,
path::{Path, PathBuf},
};

use indexmap::IndexMap;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// A newtype wrapper around a [`HashMap`], where the key is the name of the Nix
Expand All @@ -14,25 +11,25 @@ use thiserror::Error;
#[derive(Clone, Eq, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NixConfig {
settings: HashMap<String, String>,
settings: IndexMap<String, String>,
}

impl NixConfig {
pub fn new() -> Self {
Self {
settings: HashMap::new(),
settings: IndexMap::new(),
}
}

pub fn settings(&self) -> &HashMap<String, String> {
pub fn settings(&self) -> &IndexMap<String, String> {
&self.settings
}

pub fn settings_mut(&mut self) -> &mut HashMap<String, String> {
pub fn settings_mut(&mut self) -> &mut IndexMap<String, String> {
&mut self.settings
}

pub fn into_settings(self) -> HashMap<String, String> {
pub fn into_settings(self) -> IndexMap<String, String> {
self.settings
}

Expand Down Expand Up @@ -288,4 +285,40 @@ mod tests {
Some(&"https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/".into())
);
}

#[test]
fn returns_the_same_order() {
let res = NixConfig::parse_string(
r#"
cores = 32
experimental-features = flakes nix-command
max-jobs = 16
"#
.into(),
None,
);

assert!(res.is_ok());

let map = res.unwrap();

// Ensure it's not just luck that it's the same order...
for _ in 0..10 {
let settings = map.settings();

let mut settings_order = settings.into_iter();
assert_eq!(settings_order.next(), Some((&"cores".into(), &"32".into())),);
assert_eq!(
settings_order.next(),
Some((
&"experimental-features".into(),
&"flakes nix-command".into()
)),
);
assert_eq!(
settings_order.next(),
Some((&"max-jobs".into(), &"16".into())),
);
}
}
}

0 comments on commit 1dcd5d0

Please sign in to comment.