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
use crate::client::{Client, HglibError, Runner};
use crate::{runcommand, MkArg};
pub struct Arg<'a> {
pub names: &'a [&'a str],
pub untrusted: bool,
pub showsource: bool,
}
impl<'a> Default for Arg<'a> {
fn default() -> Self {
Self {
names: &[],
untrusted: false,
showsource: false,
}
}
}
impl<'a> Arg<'a> {
fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
runcommand!(
client,
"showconfig",
self.names,
"-u",
self.untrusted,
"--debug",
self.showsource
)
}
}
#[derive(Debug, PartialEq)]
pub struct Config {
pub source: Option<String>,
pub section: String,
pub key: String,
pub value: String,
}
impl Config {
fn no_source(line: &[u8]) -> Result<Self, HglibError> {
let (section, key, value) = splitline(line)?;
Ok(Self {
source: None,
section: section.to_string(),
key: key.to_string(),
value: value.to_string(),
})
}
fn with_source(source: &str, line: &[u8]) -> Result<Self, HglibError> {
let (section, key, value) = splitline(line)?;
Ok(Self {
source: Some(source.to_string()),
section: section.to_string(),
key: key.to_string(),
value: value.to_string(),
})
}
}
fn splitline(data: &[u8]) -> Result<(&str, &str, &str), HglibError> {
let data = std::str::from_utf8(data)?;
let err = || HglibError::from(format!("Invalid line in config: {}", data));
let data = data.trim_end();
let mut iter = data.rsplitn(2, '=');
let value = iter.next().ok_or_else(err)?;
let section_key = iter.next().ok_or_else(err)?;
let mut iter = section_key.splitn(2, '.');
let section = iter.next().ok_or_else(err)?;
let key = iter.next().ok_or_else(err)?;
Ok((section, key, value))
}
fn get_skv(line: &[u8]) -> Option<(&[u8], &[u8])> {
for (n, c) in line.windows(2).enumerate() {
if c == b": " {
return Some((&line[..n], &line[n + 2..]));
}
}
None
}
impl Client {
pub fn config(&mut self, x: Arg) -> Result<Vec<Config>, HglibError> {
let (data, _) = x.run(self)?;
let mut conf = Vec::new();
if x.showsource {
let mut iter = data.split(|x| *x == b'\n').filter(|x| !x.is_empty());
let mut line = iter.next();
while let Some(l) = line {
if !l.starts_with(b"read config from: ") && !l.starts_with(b"set config by: ") {
break;
}
line = iter.next();
}
while let Some(l) = line {
if let Some((source, kv)) = get_skv(l) {
let source = std::str::from_utf8(source)?;
conf.push(Config::with_source(source, kv)?);
} else {
return Err(HglibError::from(format!(
"invalid line in config: {}",
std::str::from_utf8(l)?
)));
}
line = iter.next();
}
} else {
for line in data.split(|x| *x == b'\n').filter(|x| !x.is_empty()) {
conf.push(Config::no_source(line)?);
}
}
Ok(conf)
}
}