-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathini.rs
211 lines (182 loc) · 4.52 KB
/
ini.rs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
fmt::Display, mem::swap, ops::Index
};
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize, Default)]
pub struct Ini {
pub sections: BTreeMap<String, BTreeMap<String, String>>,
}
impl Ini {
pub fn insert_section(&mut self, section: IniSection) -> Option<IniSection> {
self.sections
.insert(section.name.clone(), section.properties)
.map(|sec| (section.name, sec).into())
}
pub fn remove_section(&mut self, name: &str) -> Option<IniSection> {
self.sections
.remove(name)
.map(|sec| (name.to_owned(), sec).into())
}
}
impl Display for Ini {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self
.sections
.iter()
.map(
|(name, props)|
format!(
"[{}]\n{}",
name,
props.iter()
.map(|(k, v)| format!("{}:{}", k, v))
.collect::<Vec<_>>()
.join("\n"),
)
)
.collect::<Vec<_>>()
.join("\n"))
}
}
impl<const N: usize> From<[IniSection; N]> for Ini {
fn from(value: [IniSection; N]) -> Self {
Self {
sections: BTreeMap::from_iter(
value.into_iter()
.map(|sec| (sec.name, sec.properties))
)
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct IniSection {
name: String,
properties: BTreeMap<String, String>,
}
impl IniSection {
pub fn new(name: String) -> Self {
Self {
name,
properties: BTreeMap::default(),
}
}
pub fn insert_property(&mut self, property: IniProperty) -> Option<IniProperty> {
self.properties
.insert(property.key.clone(), property.value)
.map(|prop| (property.key, prop).into())
}
pub fn remove_property(&mut self, key: &str) -> Option<IniProperty> {
self.properties
.remove(key)
.map(|prop| (key.to_owned(), prop).into())
}
pub fn contains_key(&self, key: &str) -> bool {
self.properties
.contains_key(key)
}
pub fn value(&self, key: &str) -> Option<&String> {
self.properties
.get(key)
}
pub fn value_mut(&mut self, key: &str) -> Option<&mut String> {
self.properties
.get_mut(key)
}
}
impl Display for IniSection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}]\n{}", self.name,
self
.properties
.values()
.map(|x| x.to_string())
.collect::<Vec<_>>()
.join("\n"))
}
}
impl<const N: usize> From<(&str, [(&str, &str); N])> for IniSection {
fn from(value: (&str, [(&str, &str); N])) -> Self {
Self {
name: value.0.to_owned(),
properties: BTreeMap::from_iter(value.1.into_iter().map(|(k, v)| (k.to_owned(), v.to_owned()))),
}
}
}
impl From<(String, BTreeMap<String, String>)> for IniSection {
fn from(value: (String, BTreeMap<String, String>)) -> Self {
IniSection {
name: value.0,
properties: value.1,
}
}
}
impl From<IniSection> for (String, BTreeMap<String, String>) {
fn from(val: IniSection) -> Self {
(
val.name,
val.properties,
)
}
}
impl Index<&str> for IniSection {
type Output = String;
fn index(&self, index: &str) -> &Self::Output {
&self.properties[index]
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct IniProperty {
key: String,
value: String,
}
impl IniProperty {
pub fn key(&self) -> &str {
&self.key
}
pub fn key_mut(&mut self) -> &mut String {
&mut self.key
}
pub fn set_key(&mut self, key: String) -> String {
let mut key = key;
swap(&mut self.key, &mut key);
key
}
pub fn value(&self) -> &str {
&self.value
}
pub fn value_mut(&mut self) -> &mut String {
&mut self.value
}
pub fn set_value(&mut self, value: String) -> String {
let mut value = value;
swap(&mut self.value, &mut value);
value
}
}
impl Display for IniProperty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.key, self.value)
}
}
impl From<(String, String)> for IniProperty {
fn from(value: (String, String)) -> Self {
Self {
key: value.0,
value: value.1,
}
}
}
impl From<(&str, &str)> for IniProperty {
fn from(value: (&str, &str)) -> Self {
Self {
key: value.0.to_owned(),
value: value.1.to_owned(),
}
}
}
impl From<IniProperty> for (String, String) {
fn from(val: IniProperty) -> Self {
(val.key, val.value)
}
}