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
use config::{ConfigError, Source, Value};
use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::HashMap;
type Result<T> = std::result::Result<T, ConfigError>;
#[derive(Debug, Clone)]
pub struct CmdLine {
#[allow(dead_code)]
name: String,
contents: Vec<String>,
}
impl Default for CmdLine {
fn default() -> Self {
Self::new()
}
}
impl CmdLine {
pub fn new() -> Self {
CmdLine {
name: "command line".to_string(),
contents: Vec::new(),
}
}
pub fn push_toml_line(&mut self, line: String) {
self.contents.push(line);
}
fn convert_toml_error(&self, s: &str, pos: Option<(usize, usize)>) -> String {
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^(.*?) at line [0-9]+ column [0-9]+$").expect("Can't compile regex")
});
let cap = RE.captures(s);
let msg = match cap {
Some(c) => c.get(1).expect("mismatch regex: no capture group").as_str(),
None => s,
};
let location = match pos {
Some((line, _col)) if line < self.contents.len() => {
format!(" in {:?}", self.contents[line])
}
_ => " on command line".to_string(),
};
format!("{}{}", msg, location)
}
fn build_toml(&self) -> String {
let mut toml_s = String::new();
for line in &self.contents {
toml_s.push_str(tweak_toml_bareword(line).as_ref().unwrap_or(line));
toml_s.push('\n');
}
toml_s
}
}
impl Source for CmdLine {
fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
Box::new(self.clone())
}
fn collect(&self) -> Result<HashMap<String, Value>> {
let toml_s = self.build_toml();
let toml_v: toml::Value = match toml::from_str(&toml_s) {
Err(e) => {
return Err(ConfigError::Message(
self.convert_toml_error(&e.to_string(), e.line_col()),
))
}
Ok(v) => v,
};
toml_v
.try_into()
.map_err(|e| ConfigError::Foreign(Box::new(e)))
}
}
fn tweak_toml_bareword(s: &str) -> Option<String> {
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r#"(?x:
^
[ \t]*
# first capture group: dotted barewords
((?:[a-zA-Z0-9_\-]+\.)*
[a-zA-Z0-9_\-]+)
[ \t]*=[ \t]*
# second group: one bareword without hyphens
([a-zA-Z0-9_]+)
[ \t]*
$)"#,
)
.expect("Built-in regex compilation failed")
});
RE.captures(s).map(|c| format!("{}=\"{}\"", &c[1], &c[2]))
}
#[cfg(test)]
mod test {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn bareword_expansion() {
assert_eq!(tweak_toml_bareword("dsfklj"), None);
assert_eq!(tweak_toml_bareword("=99"), None);
assert_eq!(tweak_toml_bareword("=[1,2,3]"), None);
assert_eq!(tweak_toml_bareword("a=b-c"), None);
assert_eq!(tweak_toml_bareword("a=bc"), Some("a=\"bc\"".into()));
assert_eq!(tweak_toml_bareword("a=b_c"), Some("a=\"b_c\"".into()));
assert_eq!(
tweak_toml_bareword("hello.there.now=a_greeting"),
Some("hello.there.now=\"a_greeting\"".into())
);
}
#[test]
fn conv_toml_error() {
let mut cl = CmdLine::new();
cl.push_toml_line("Hello=world".to_string());
cl.push_toml_line("Hola=mundo".to_string());
cl.push_toml_line("Bonjour=monde".to_string());
assert_eq!(
&cl.convert_toml_error("Nice greeting at line 1 column 1", Some((0, 1))),
"Nice greeting in \"Hello=world\""
);
assert_eq!(
&cl.convert_toml_error("Nice greeting at line 1 column 1", Some((7, 1))),
"Nice greeting on command line"
);
assert_eq!(
&cl.convert_toml_error("Nice greeting with a thing", Some((0, 1))),
"Nice greeting with a thing in \"Hello=world\""
);
}
#[test]
fn clone_into_box() {
let mut cl = CmdLine::new();
cl.push_toml_line("Molo=Lizwe".to_owned());
let cl2 = cl.clone_into_box();
let v = cl2.collect().unwrap();
assert_eq!(v["Molo"], "Lizwe".into());
}
#[test]
fn parse_good() {
let mut cl = CmdLine::default();
cl.push_toml_line("a=3".to_string());
cl.push_toml_line("bcd=hello".to_string());
cl.push_toml_line("ef=\"gh i\"".to_string());
cl.push_toml_line("w=[1,2,3]".to_string());
let v = cl.collect().unwrap();
assert_eq!(v["a"], "3".into());
assert_eq!(v["bcd"], "hello".into());
assert_eq!(v["ef"], "gh i".into());
assert_eq!(v["w"], vec![1, 2, 3].into());
}
#[test]
fn parse_bad() {
let mut cl = CmdLine::default();
cl.push_toml_line("x=1 1 1 1 1".to_owned());
let v = cl.collect();
assert!(v.is_err());
}
}