subst/features/
indexmap.rs1use indexmap::IndexMap;
2
3use crate::VariableMap;
4
5impl<'a, V: 'a> VariableMap<'a> for IndexMap<&str, V> {
6 type Value = &'a V;
7
8 #[inline]
9 fn get(&'a self, key: &str) -> Option<Self::Value> {
10 self.get(key)
11 }
12}
13
14impl<'a, V: 'a> VariableMap<'a> for IndexMap<String, V> {
15 type Value = &'a V;
16
17 #[inline]
18 fn get(&'a self, key: &str) -> Option<Self::Value> {
19 self.get(key)
20 }
21}
22
23#[cfg(test)]
24#[rustfmt::skip]
25mod test {
26 use indexmap::IndexMap;
27 use assert2::check;
28
29 use crate::{substitute, substitute_bytes};
30
31 #[test]
32 fn test_substitute() {
33 let mut map: IndexMap<String, String> = IndexMap::new();
34 map.insert("name".into(), "world".into());
35 check!(let Ok("Hello world!") = substitute("Hello $name!", &map).as_deref());
36 check!(let Ok("Hello world!") = substitute("Hello ${name}!", &map).as_deref());
37 check!(let Ok("Hello world!") = substitute("Hello ${name:not-world}!", &map).as_deref());
38 check!(let Ok("Hello world!") = substitute("Hello ${not_name:world}!", &map).as_deref());
39
40 let mut map: IndexMap<&str, &str> = IndexMap::new();
41 map.insert("name", "world");
42 check!(let Ok("Hello world!") = substitute("Hello $name!", &map).as_deref());
43 check!(let Ok("Hello world!") = substitute("Hello ${name}!", &map).as_deref());
44 check!(let Ok("Hello world!") = substitute("Hello ${name:not-world}!", &map).as_deref());
45 check!(let Ok("Hello world!") = substitute("Hello ${not_name:world}!", &map).as_deref());
46 }
47
48 #[test]
49 fn test_substitute_bytes() {
50 let mut map: IndexMap<String, Vec<u8>> = IndexMap::new();
51 map.insert("name".into(), b"world"[..].into());
52 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello $name!", &map).as_deref());
53 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name}!", &map).as_deref());
54 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name:not-world}!", &map).as_deref());
55 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${not_name:world}!", &map).as_deref());
56
57 let mut map: IndexMap<&str, &[u8]> = IndexMap::new();
58 map.insert("name", b"world");
59 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello $name!", &map).as_deref());
60 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name}!", &map).as_deref());
61 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${name:not-world}!", &map).as_deref());
62 check!(let Ok(b"Hello world!") = substitute_bytes(b"Hello ${not_name:world}!", &map).as_deref());
63 }
64}