1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use dynfmt::{Argument, FormatArgs};
use std::collections::HashMap;
#[derive(Clone, Default, Debug)]
pub struct Parameters(HashMap<String, String>);
const TEXT_KEY: &str = "text";
impl FormatArgs for Parameters {
fn get_index(&self, index: usize) -> Result<Option<Argument<'_>>, ()> {
if index == 0 {
self.get_key(TEXT_KEY)
} else {
self.0.get_index(index)
}
}
fn get_key(&self, key: &str) -> Result<Option<Argument<'_>>, ()> {
self.0.get_key(key)
}
}
impl Parameters {
pub fn new() -> Parameters {
Parameters(HashMap::new())
}
pub fn new_with_text<T: Into<String>>(text: T) -> Parameters {
let mut map = HashMap::new();
map.insert(TEXT_KEY.to_string(), text.into());
Parameters(map)
}
pub fn with<K: Into<String>, V: Into<String>>(&self, key: K, value: V) -> Parameters {
let mut copy = self.clone();
copy.0.insert(key.into(), value.into());
copy
}
pub fn with_text<K: Into<String>>(&self, text: K) -> Parameters {
self.with(TEXT_KEY, text)
}
pub fn combine(&self, other: &Parameters) -> Parameters {
let mut copy = self.clone();
copy.0.extend(other.0.clone());
copy
}
pub fn get(&self, key: &str) -> Option<&String> {
self.0.get(key)
}
}
impl From<String> for Parameters {
fn from(text: String) -> Self {
Parameters::new_with_text(text)
}
}
impl From<&str> for Parameters {
fn from(text: &str) -> Self {
Parameters::new_with_text(text)
}
}
impl From<HashMap<String, String>> for Parameters {
fn from(map: HashMap<String, String>) -> Self {
Parameters(map)
}
}
impl From<Vec<(String, String)>> for Parameters {
fn from(data: Vec<(String, String)>) -> Self {
let map: HashMap<String, String> = data.into_iter().collect();
Parameters(map)
}
}
impl From<Vec<(&str, &str)>> for Parameters {
fn from(data: Vec<(&str, &str)>) -> Self {
let map: HashMap<String, String> = data
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
Parameters(map)
}
}