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

Add support for the indexmap crate. #25

Merged
merged 2 commits into from
Sep 13, 2024
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --workspace --all-targets --features json,toml,yaml --color=always
args: --workspace --all-targets --features indexmap,json,toml,yaml --color=always
- name: Test
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --all-targets --features json,toml,yaml --color=always
args: --workspace --all-targets --features indexmap,json,toml,yaml --color=always
- name: Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --workspace --all-targets --features json,toml,yaml
args: --workspace --all-targets --features indexmap,json,toml,yaml
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Unreleased
- [add][minor] Add optional support for the `indexmap` crate.

# Version 0.3.3 - 2024-06-29
- [add][minor] Add support for substitution in all string values of TOML data.
- [add][minor] Add support for substitution in all string values of JSON data.
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ categories = ["template-engine", "value-formatting"]
edition = "2021"

[features]
# Implement `VariableMap` for `indexmap::IndexMap`.
indexmap = ["dep:indexmap"]

# Enable support for performing substitution in all string values of a JSON document.
json = ["dep:serde", "dep:serde_json"]

Expand All @@ -30,6 +33,7 @@ preserve-order = ["toml?/preserve_order", "serde_json?/preserve_order"]
doc-cfg = []

[dependencies]
indexmap = { version = "2.5.0", optional = true }
memchr = "2.4.1"
serde = { version = "1.0.0", optional = true }
serde_json = { version = "1.0.118", optional = true }
Expand Down
64 changes: 64 additions & 0 deletions src/features/indexmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use indexmap::IndexMap;

use crate::VariableMap;

impl<'a, V: 'a> VariableMap<'a> for IndexMap<&str, V> {
type Value = &'a V;

#[inline]
fn get(&'a self, key: &str) -> Option<Self::Value> {
self.get(key)
}
}

impl<'a, V: 'a> VariableMap<'a> for IndexMap<String, V> {
type Value = &'a V;

#[inline]
fn get(&'a self, key: &str) -> Option<Self::Value> {
self.get(key)
}
}

#[cfg(test)]
#[rustfmt::skip]
mod test {
use indexmap::IndexMap;
use assert2::check;

use crate::{substitute, substitute_bytes};

#[test]
fn test_substitute() {
let mut map: IndexMap<String, String> = IndexMap::new();
map.insert("name".into(), "world".into());
check!(let Ok("Hello world!") = substitute("Hello $name!", &map).as_deref());
check!(let Ok("Hello world!") = substitute("Hello ${name}!", &map).as_deref());
check!(let Ok("Hello world!") = substitute("Hello ${name:not-world}!", &map).as_deref());
check!(let Ok("Hello world!") = substitute("Hello ${not_name:world}!", &map).as_deref());

let mut map: IndexMap<&str, &str> = IndexMap::new();
map.insert("name", "world");
check!(let Ok("Hello world!") = substitute("Hello $name!", &map).as_deref());
check!(let Ok("Hello world!") = substitute("Hello ${name}!", &map).as_deref());
check!(let Ok("Hello world!") = substitute("Hello ${name:not-world}!", &map).as_deref());
check!(let Ok("Hello world!") = substitute("Hello ${not_name:world}!", &map).as_deref());
}

#[test]
fn test_substitute_bytes() {
let mut map: IndexMap<String, Vec<u8>> = IndexMap::new();
map.insert("name".into(), b"world"[..].into());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello $name!", &map).as_deref());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name}!", &map).as_deref());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name:not-world}!", &map).as_deref());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${not_name:world}!", &map).as_deref());

let mut map: IndexMap<&str, &[u8]> = IndexMap::new();
map.insert("name", b"world");
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello $name!", &map).as_deref());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name}!", &map).as_deref());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name:not-world}!", &map).as_deref());
check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${not_name:world}!", &map).as_deref());
}
}
File renamed without changes.
15 changes: 15 additions & 0 deletions src/features/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[cfg(feature = "indexmap")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "indexmap")))]
mod indexmap;

#[cfg(feature = "json")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "json")))]
pub mod json;

#[cfg(feature = "yaml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "yaml")))]
pub mod yaml;

#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
pub mod toml;
File renamed without changes.
File renamed without changes.
14 changes: 3 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,9 @@ pub use map::*;
mod template;
pub use template::*;

#[cfg(feature = "json")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "json")))]
pub mod json;

#[cfg(feature = "yaml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "yaml")))]
pub mod yaml;

#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
pub mod toml;
mod features;
#[allow(unused_imports)] // Might not re-export anything if all features are disabled.
pub use features::*;

/// Substitute variables in a string.
///
Expand Down
Loading