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
use std::collections::HashMap;
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct KV {
pub key: String,
pub value: String,
}
impl KV {
pub fn new(key: String, value: String) -> Self {
KV { key, value }
}
pub fn normalise_key(&mut self, prefix: &str) {
let mut pfx = prefix.to_string();
pfx.push('_');
self.key = self.key.clone().replace(&pfx, "").to_ascii_lowercase()
}
pub fn toml(&self) -> String {
format!("{} = '{}'", self.key, self.value)
}
}
#[derive(Clone, Debug, Default)]
pub struct KVMap {
pub top_level: String,
pub data: HashMap<String, Vec<KV>>,
}
impl KVMap {
pub fn new(top_level: &str) -> Self {
KVMap {
top_level: top_level.to_string(),
..Default::default()
}
}
pub fn insert(&mut self, section: Option<&String>, kvs: Vec<KV>) {
self.data.insert(section.unwrap().to_string(), kvs);
}
pub fn keys(&self) -> Vec<String> {
let mut keys: Vec<String> = self.data.clone().into_keys().collect();
keys.sort();
keys
}
pub fn values(&self) -> Vec<Vec<KV>> {
let mut vals: Vec<Vec<KV>> = self.data.clone().into_values().collect();
vals.sort();
vals
}
pub fn section(&self, name: &str) -> Vec<KV> {
let mut s = self.data.get(name).unwrap().to_vec();
s.sort_by(|a, b| a.key.cmp(&b.key));
s
}
pub fn section_toml(&self, name: &str) -> String {
let mut toml = String::new();
if name != self.top_level {
toml.push_str(&format!("[{name}]\n"))
}
let mut body = self
.section(name)
.iter()
.map(|kv| kv.toml())
.collect::<Vec<String>>();
body.sort();
toml.push_str(&body.join("\n"));
toml
}
pub fn toml(&self) -> String {
let mut toml = self.section_toml(&self.top_level);
toml.push('\n');
for key in self.keys() {
if key != self.top_level {
toml.push('\n');
toml.push_str(&self.section_toml(&key));
toml.push('\n');
}
}
toml
}
}