Skip to content

Commit

Permalink
Add optional support for the indexmap crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Sep 13, 2024
1 parent cdf6832 commit 35c4f92
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
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());
}
}
4 changes: 4 additions & 0 deletions src/features/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[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;
Expand Down

0 comments on commit 35c4f92

Please sign in to comment.