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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as Map;
use alloc::sync::Arc;
use core::fmt;
#[cfg(feature = "std")]
use std::collections::HashMap as Map;
/// File and line information
#[derive(Clone, Debug)]
pub struct Location {
file: Arc<str>,
line: u64,
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.file, self.line)
}
}
/// A string value, together with location information about where it is defined.
#[derive(Clone, Debug)]
struct Value {
value: Box<str>,
defined_at: Location,
}
#[derive(Clone, Debug, Default)]
pub struct Configuration {
// wrap in an Arc for cheap cloning
children: Arc<Map<Box<str>, Configuration>>,
values: Arc<Map<Box<str>, Value>>,
}
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "std")]
IO(Box<str>, std::io::Error),
IllegalCharacter(Location, char),
InvalidUtf8(Location),
BadInt(Location, Box<str>),
BadUInt(Location, Box<str>),
BadFloat(Location, Box<str>),
BadBool(Location, Box<str>),
UnmatchedLeftBrace(Location),
InvalidKey(Location, Box<str>),
InvalidValue(Location),
InvalidLine(Location),
StrayCharsAfterQuotedString(Location),
UnterminatedString(Location, char),
}
impl fmt::Display for Error {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
todo!()
}
}
pub type Result<T> = std::result::Result<T, Error>;
fn parse_int(location: &Location, string: &str) -> Result<i64> {
let bad_int = || Error::BadInt(location.clone(), string.into());
if !string
.bytes()
.all(|c| c.is_ascii_hexdigit() || c == b'x' || c == b'X' || c == b'-' || c == b'+')
{
return Err(bad_int());
}
let signless = string.strip_prefix(['-', '+']).unwrap_or(string);
let mut base = 10;
let baseless = signless
.strip_prefix("0x")
.or_else(|| signless.strip_prefix("0X"))
.unwrap_or_else(|| {
base = 16;
signless
});
for digit in baseless.bytes() {
if base == 10 && !digit.is_ascii_digit() {
return Err(bad_int());
}
}
string.parse().map_err(|_| bad_int())
}
fn parse_uint(location: &Location, string: &str) -> Result<u64> {
let bad_uint = || Error::BadUInt(location.clone(), string.into());
if !string
.bytes()
.all(|c| c.is_ascii_hexdigit() || c == b'x' || c == b'X' || c == b'+')
{
return Err(bad_uint());
}
let signless = string.strip_prefix('+').unwrap_or(string);
let mut base = 10;
let baseless = signless
.strip_prefix("0x")
.or_else(|| signless.strip_prefix("0X"))
.unwrap_or_else(|| {
base = 16;
signless
});
for digit in baseless.bytes() {
if base == 10 && !digit.is_ascii_digit() {
return Err(bad_uint());
}
}
let val = signless.parse().map_err(|_| bad_uint())?;
if val > i64::MAX as u64 {
return Err(bad_uint());
}
Ok(val)
}
fn parse_float(location: &Location, string: &str) -> Result<f64> {
let bad_float = || Error::BadFloat(location.clone(), string.into());
if !string.bytes().all(|c| {
c.is_ascii_digit() || c == b'.' || c == b'+' || c == b'-' || c == b'e' || c == b'E'
}) {
return Err(bad_float());
}
string.parse().map_err(|_| bad_float())
}
fn parse_bool(location: &Location, string: &str) -> Result<bool> {
match string {
"yes" | "on" | "true" => Ok(true),
"no" | "off" | "false" => Ok(false),
_ => Err(Error::BadBool(location.clone(), string.into())),
}
}
fn parse_list(_location: &Location, _string: &str) -> Vec<String> {
todo!()
}
/// Trait for reading configurations.
///
/// Ordinarily you won't need to implement this trait, since it is
/// already implemented by any `T: `[`std::io::BufRead`] (or else `&str` and `&[u8]`,
/// if the `std` feature is not enabled).
pub trait Read {
/// Read up to the next line feed (or EOF), not including the line feed itself.
///
/// Puts the line in `line` and returns `Ok(true)`, if the end of file has been reached,
/// `line` is unmodified and `Ok(false)` is returned.
///
/// You don't need to check for valid UTF-8 here — that is already done in the code which uses
/// this trait.
fn read_until_lf(&mut self, line: &mut Vec<u8>) -> Result<bool>;
}
#[cfg(feature = "std")]
impl<R: std::io::BufRead> Read for R {
fn read_until_lf(&mut self, line: &mut Vec<u8>) -> Result<bool> {
self.read_until(b'\n', line)
.map_err(|e| Error::IO("read error".into(), e))?;
if line.ends_with(b"\n") {
line.pop();
Ok(true)
} else {
Ok(!line.is_empty())
}
}
}
#[cfg(not(feature = "std"))]
impl Read for &str {
fn read_until_lf(&mut self, line: &mut Vec<u8>) -> Result<bool> {
match self.split_once('\n') {
Some((pre, post)) => {
*self = post;
line.extend_from_slice(pre.as_bytes());
Ok(true)
}
None => {
if self.is_empty() {
return Ok(false);
}
line.extend_from_slice(self.as_bytes());
*self = "";
Ok(true)
}
}
}
}
#[cfg(not(feature = "std"))]
impl Read for &[u8] {
fn read_until_lf(&mut self, line: &mut Vec<u8>) -> Result<bool> {
match self.iter().position(|&c| c == b'\n') {
Some(i) => {
line.extend_from_slice(&self[..i]);
*self = &self[i + 1..];
Ok(true)
}
None => {
if self.is_empty() {
return Ok(false);
}
line.extend_from_slice(self);
*self = b"";
Ok(true)
}
}
}
}
fn check_valid_key(location: &Location, s: &str) -> Result<()> {
if s.bytes().all(|c| {
c >= 0x80 || c.is_ascii_alphanumeric() || matches!(c, b'.' | b'_' | b'/' | b'*' | b'-')
}) {
Ok(())
} else {
Err(Error::InvalidKey(location.clone(), s.into()))
}
}
/// Returns (unquoted value, new line number)
fn read_quoted_value(
quoted: &str,
reader: &mut dyn Read,
start_location: &Location,
) -> Result<(String, u64)> {
let delimiter: char = quoted.chars().next().unwrap();
let mut unquoted = String::new();
let mut line_number = start_location.line;
let location = |line_number: u64| Location {
file: start_location.file.clone(),
line: line_number,
};
let mut line_buf = vec![];
let mut first = true;
loop {
let line = if first {
first = false;
quoted
} else {
line_buf.truncate(0);
if !reader.read_until_lf(&mut line_buf)? {
break;
}
line_number += 1;
line_buf.pop_if(|c| *c == b'\r');
str::from_utf8(&line_buf).map_err(|_| Error::InvalidUtf8(location(line_number)))?
};
let mut chars = line.chars();
while let Some(c) = chars.next() {
if c == delimiter {
if !chars.all(|c| c == ' ' || c == '\t') {
return Err(Error::StrayCharsAfterQuotedString(location(line_number)));
}
return Ok((unquoted, line_number));
} else if c == '\\' {
todo!() // parse escape sequence
} else if c == '\0' {
return Err(Error::InvalidValue(location(line_number)));
} else {
unquoted.push(c);
}
}
}
Err(Error::UnterminatedString(start_location.clone(), delimiter))
}
impl Configuration {
fn load_dyn(filename: &str, reader: &mut dyn Read) -> Result<Self> {
let mut config = Configuration::default();
let mut line = vec![];
let mut line_number = 0;
let mut current_section = &mut config;
let filename: Arc<str> = filename.into();
loop {
line.truncate(0);
if !reader.read_until_lf(&mut line)? {
break;
}
line_number += 1;
let location = Location {
file: filename.clone(),
line: line_number,
};
line.pop_if(|c| *c == b'\r');
for c in &line {
if (0..0x1f).contains(c) && *c != b'\t' {
return Err(Error::IllegalCharacter(location, char::from(*c)));
}
}
let mut line =
str::from_utf8(&line).map_err(|_| Error::InvalidUtf8(location.clone()))?;
line = line.trim_start_matches(['\t', ' ']);
if line.is_empty() || line.starts_with('#') {
// comment/blank line
continue;
}
if line.starts_with('[') {
line = line.trim_end_matches(['\t', ' ']);
if !line.ends_with(']') {
return Err(Error::UnmatchedLeftBrace(location));
}
let new_section = line[1..line.len() - 1].into();
current_section = &mut config;
check_valid_key(&location, new_section)?;
if !new_section.is_empty() {
for component in new_section.split('.') {
current_section = Arc::get_mut(&mut current_section.children)
.unwrap()
.entry(component.into())
.or_default();
}
}
} else {
let (mut relative_key, mut value) = line
.split_once('=')
.ok_or_else(|| Error::InvalidLine(location.clone()))?;
check_valid_key(&location, relative_key)?;
relative_key = relative_key.trim_end_matches(['\t', ' ']);
value = value.trim_start_matches(['\t', ' ']);
fn insert(
mut section: &mut Configuration,
location: Location,
mut key: &str,
value: &str,
) {
if let Some(last_dot) = key.rfind('.') {
for component in key[..last_dot].split('.') {
section = Arc::get_mut(&mut section.children)
.unwrap()
.entry(component.into())
.or_default()
}
key = &key[last_dot + 1..];
}
Arc::get_mut(&mut section.values).unwrap().insert(
key.into(),
Value {
value: value.into(),
defined_at: location,
},
);
}
if value.starts_with(['`', '"', '\'']) {
let (value, new_line_number) = read_quoted_value(value, reader, &location)?;
insert(current_section, location, relative_key, &value);
line_number = new_line_number;
} else {
value = value.trim_end_matches(['\t', ' ']);
if value.contains('\0') {
return Err(Error::InvalidValue(location));
}
insert(current_section, location, relative_key, value);
}
}
}
Ok(config)
}
pub fn load<R: Read>(filename: &str, mut reader: R) -> Result<Self> {
// avoid big code size by using dyn reference.
// the impact on performance is not really important.
Configuration::load_dyn(filename, &mut reader)
}
#[cfg(feature = "std")]
pub fn load_path<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
let p = path.as_ref();
let filename = p.to_string_lossy();
let file = std::fs::File::open(p).map_err(|e| Error::IO(filename.clone().into(), e))?;
Configuration::load(&filename, std::io::BufReader::new(file))
}
pub fn section(&self, key: &str) -> Configuration {
let mut node = self;
for component in key.split('.') {
node = match self.children.get(component) {
Some(x) => x,
None => return Configuration::default(),
};
}
node.clone()
}
fn get_val(&self, key: &str) -> Option<&Value> {
let Some(last_dot) = key.rfind('.') else {
return self.values.get(key);
};
let (path, last_component) = key.split_at(last_dot);
let mut node = self;
for component in path.split('.') {
node = self.children.get(component)?;
}
node.values.get(last_component)
}
pub fn get(&self, key: &str) -> Option<&str> {
Some(self.get_val(key)?.value.as_ref())
}
pub fn location(&self, key: &str) -> Option<Location> {
Some(self.get_val(key)?.defined_at.clone())
}
pub fn has(&self, key: &str) -> bool {
self.get(key).is_some()
}
pub fn get_or_default<'a>(&'a self, key: &str, default: &'a str) -> &'a str {
self.get(key).unwrap_or(default)
}
pub fn get_int(&self, key: &str) -> Option<Result<i64>> {
let Value { value, defined_at } = self.get_val(key)?;
Some(parse_int(defined_at, value.as_ref()))
}
pub fn get_int_or_default(&self, key: &str, default: i64) -> Result<i64> {
self.get_int(key).unwrap_or(Ok(default))
}
pub fn get_uint(&self, key: &str) -> Option<Result<u64>> {
let Value { value, defined_at } = self.get_val(key)?;
Some(parse_uint(defined_at, value.as_ref()))
}
pub fn get_uint_or_default(&self, key: &str, default: u64) -> Result<u64> {
self.get_uint(key).unwrap_or(Ok(default))
}
pub fn get_float(&self, key: &str) -> Option<Result<f64>> {
let Value { value, defined_at } = self.get_val(key)?;
Some(parse_float(defined_at, value.as_ref()))
}
pub fn get_float_or_default(&self, key: &str, default: f64) -> Result<f64> {
self.get_float(key).unwrap_or(Ok(default))
}
pub fn get_bool(&self, key: &str) -> Option<Result<bool>> {
let Value { value, defined_at } = self.get_val(key)?;
Some(parse_bool(defined_at, value.as_ref()))
}
pub fn get_bool_or_default(&self, key: &str, default: bool) -> Result<bool> {
self.get_bool(key).unwrap_or(Ok(default))
}
pub fn get_list(&self, key: &str) -> Option<Vec<String>> {
let Value { value, defined_at } = self.get_val(key)?;
Some(parse_list(defined_at, value.as_ref()))
}
pub fn get_list_or_default(
&self,
key: &str,
default: impl FnOnce() -> Vec<String>,
) -> Vec<String> {
self.get_list(key).unwrap_or_else(default)
}
/// Merge `conf` into `self`, preferring values in `conf`.
pub fn merge(&mut self, conf: &Configuration) {
let new_values = Arc::make_mut(&mut self.values);
// merge conf.values into self.values
for (key, val) in conf.values.iter() {
new_values.insert(key.clone(), val.clone());
}
// merge conf.children into self.children
let new_children = Arc::make_mut(&mut self.children);
for (key, child) in conf.children.iter() {
new_children.entry(key.clone()).or_default().merge(child)
}
}
}
|