1
//! Implement a configuration source based on command-line arguments.
2

            
3
use regex::Regex;
4
use std::sync::LazyLock;
5

            
6
/// A CmdLine holds a set of command-line arguments that augment a
7
/// configuration.
8
///
9
/// These arguments are formatted in toml, and concatenated into a
10
/// single toml object.  With arguments of the form "key=bareword",
11
/// the bareword is quoted for convenience.
12
#[derive(Debug, Clone)]
13
pub struct CmdLine {
14
    /// String for decorating Values.
15
    //
16
    // TODO(nickm): not yet used.
17
    #[allow(dead_code)]
18
    name: String,
19
    /// List of toml lines as given on the command line.
20
    contents: Vec<String>,
21
}
22

            
23
impl Default for CmdLine {
24
4
    fn default() -> Self {
25
4
        Self::new()
26
4
    }
27
}
28

            
29
impl CmdLine {
30
    /// Make a new empty command-line
31
4473
    pub fn new() -> Self {
32
4473
        CmdLine {
33
4473
            name: "command line".to_string(),
34
4473
            contents: Vec::new(),
35
4473
        }
36
4473
    }
37
    /// Add a single line of toml to the configuration.
38
2954
    pub fn push_toml_line(&mut self, line: String) {
39
2954
        self.contents.push(line);
40
2954
    }
41

            
42
    /// Try to adjust the contents of a toml deserialization error so
43
    /// that instead it refers to a single command-line argument.
44
    #[allow(clippy::string_slice)] // TODO
45
8
    fn convert_toml_error(
46
8
        &self,
47
8
        toml_str: &str,
48
8
        error_message: &str,
49
8
        span: &Option<std::ops::Range<usize>>,
50
8
    ) -> String {
51
        // Function to translate a string index to a 0-offset line number.
52
254
        let linepos = |idx| toml_str.bytes().take(idx).filter(|b| *b == b'\n').count();
53

            
54
        // Find the source position as a line within toml_str, and convert that
55
        // to an index into self.contents.
56
8
        let source_line = span
57
8
            .as_ref()
58
12
            .and_then(|range| {
59
8
                let startline = linepos(range.start);
60
8
                let endline = linepos(range.end);
61
8
                (startline == endline).then_some(startline)
62
8
            })
63
12
            .and_then(|pos| self.contents.get(pos));
64

            
65
8
        match (source_line, span.as_ref()) {
66
6
            (Some(source), _) => {
67
6
                format!("Couldn't parse command line: {error_message} in {source:?}")
68
            }
69
2
            (None, Some(range)) if toml_str.get(range.clone()).is_some() => format!(
70
                "Couldn't parse command line: {error_message} within {:?}",
71
                &toml_str[range.clone()]
72
            ),
73
2
            _ => format!("Couldn't parse command line: {error_message}"),
74
        }
75
8
    }
76

            
77
    /// Compose elements of this cmdline into a single toml string.
78
4473
    fn build_toml(&self) -> String {
79
4473
        let mut toml_s = String::new();
80
4479
        for line in &self.contents {
81
2954
            toml_s.push_str(tweak_toml_bareword(line).as_ref().unwrap_or(line));
82
2954
            toml_s.push('\n');
83
2954
        }
84
4473
        toml_s
85
4473
    }
86
}
87

            
88
impl figment::Provider for CmdLine {
89
4467
    fn metadata(&self) -> figment::Metadata {
90
4467
        figment::Metadata::named("command line")
91
4467
    }
92

            
93
4471
    fn data(&self) -> figment::Result<figment::value::Map<figment::Profile, figment::value::Dict>> {
94
4471
        let toml_str = self.build_toml();
95
4472
        let toml: toml::Value = toml::from_str(&toml_str).map_err(|toml_err| {
96
2
            self.convert_toml_error(&toml_str, toml_err.message(), &toml_err.span())
97
3
        })?;
98

            
99
4469
        figment::providers::Serialized::defaults(toml).data()
100
4471
    }
101
}
102

            
103
/// If `s` is a string of the form "keyword=bareword", return a new string
104
/// where `bareword` is quoted. Otherwise return None.
105
///
106
/// This isn't a smart transformation outside the context of 'config',
107
/// since many serde formats don't do so good a job when they get a
108
/// string when they wanted a number or whatever.  But 'config' is
109
/// pretty happy to convert strings to other stuff.
110
2968
fn tweak_toml_bareword(s: &str) -> Option<String> {
111
    /// Regex to match a keyword=bareword item.
112
2564
    static RE: LazyLock<Regex> = LazyLock::new(|| {
113
2564
        Regex::new(
114
2564
            r#"(?x:
115
2564
               ^
116
2564
                [ \t]*
117
2564
                # first capture group: dotted barewords
118
2564
                ((?:[a-zA-Z0-9_\-]+\.)*
119
2564
                 [a-zA-Z0-9_\-]+)
120
2564
                [ \t]*=[ \t]*
121
2564
                # second group: a string without hyphens (e.g. bareword, a disk path, etc.)
122
2564
                ([a-zA-Z0-9_:\./\\]+)
123
2564
                [ \t]*
124
2564
                $)"#,
125
        )
126
2564
        .expect("Built-in regex compilation failed")
127
2564
    });
128

            
129
2981
    RE.captures(s).map(|c| format!("{}=\"{}\"", &c[1], &c[2]))
130
2968
}
131

            
132
#[cfg(test)]
133
mod test {
134
    // @@ begin test lint list maintained by maint/add_warning @@
135
    #![allow(clippy::bool_assert_comparison)]
136
    #![allow(clippy::clone_on_copy)]
137
    #![allow(clippy::dbg_macro)]
138
    #![allow(clippy::mixed_attributes_style)]
139
    #![allow(clippy::print_stderr)]
140
    #![allow(clippy::print_stdout)]
141
    #![allow(clippy::single_char_pattern)]
142
    #![allow(clippy::unwrap_used)]
143
    #![allow(clippy::unchecked_time_subtraction)]
144
    #![allow(clippy::useless_vec)]
145
    #![allow(clippy::needless_pass_by_value)]
146
    #![allow(clippy::string_slice)] // See arti#2571
147
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
148
    use super::*;
149
    use figment::Provider as _;
150

            
151
    #[test]
152
    fn bareword_expansion() {
153
        assert_eq!(tweak_toml_bareword("dsfklj"), None);
154
        assert_eq!(tweak_toml_bareword("=99"), None);
155
        assert_eq!(tweak_toml_bareword("=[1,2,3]"), None);
156
        assert_eq!(tweak_toml_bareword("a=b-c"), None);
157

            
158
        assert_eq!(tweak_toml_bareword("a=bc"), Some("a=\"bc\"".into()));
159
        assert_eq!(tweak_toml_bareword("a=b_c"), Some("a=\"b_c\"".into()));
160
        assert_eq!(
161
            tweak_toml_bareword("hello.there.now=a_greeting"),
162
            Some("hello.there.now=\"a_greeting\"".into())
163
        );
164
    }
165

            
166
    #[test]
167
    fn conv_toml_error() {
168
        let mut cl = CmdLine::new();
169
        cl.push_toml_line("Hello=world".to_string());
170
        cl.push_toml_line("Hola=mundo".to_string());
171
        cl.push_toml_line("Bonjour=monde".to_string());
172
        let toml_s = cl.build_toml();
173

            
174
        assert_eq!(
175
            &cl.convert_toml_error(&toml_s, "Nice greeting", &Some(0..13)),
176
            "Couldn't parse command line: Nice greeting in \"Hello=world\""
177
        );
178

            
179
        assert_eq!(
180
            &cl.convert_toml_error(&toml_s, "Nice greeting", &Some(99..333)),
181
            "Couldn't parse command line: Nice greeting"
182
        );
183

            
184
        assert_eq!(
185
            &cl.convert_toml_error(&toml_s, "Nice greeting with a thing", &Some(0..13)),
186
            "Couldn't parse command line: Nice greeting with a thing in \"Hello=world\""
187
        );
188
    }
189

            
190
    #[test]
191
    fn parse_good() {
192
        let mut cl = CmdLine::default();
193
        cl.push_toml_line("a=3".to_string());
194
        cl.push_toml_line("bcd=hello".to_string());
195
        cl.push_toml_line("ef=\"gh i\"".to_string());
196
        cl.push_toml_line("w=[1,2,3]".to_string());
197
        cl.push_toml_line("dir1=.".to_string());
198
        cl.push_toml_line("dir2=../".to_string());
199
        cl.push_toml_line("dir3=../my_directory".to_string());
200
        cl.push_toml_line("dir4=C:\\\\temp\\\\arti".to_string());
201

            
202
        let v = cl
203
            .data()
204
            .unwrap()
205
            .remove(&figment::Profile::Default)
206
            .unwrap();
207

            
208
        assert_eq!(v["a"], "3".into());
209
        assert_eq!(v["bcd"], "hello".into());
210
        assert_eq!(v["ef"], "gh i".into());
211
        assert_eq!(v["w"], vec![1, 2, 3].into());
212
        assert_eq!(v["dir1"], ".".into());
213
        assert_eq!(v["dir2"], "../".into());
214
        assert_eq!(v["dir3"], "../my_directory".into());
215
        assert_eq!(v["dir4"], "C:\\temp\\arti".into());
216
    }
217

            
218
    #[test]
219
    fn parse_bad() {
220
        let mut cl = CmdLine::default();
221
        cl.push_toml_line("x=1 1 1 1 1".to_owned());
222
        let v = cl.data();
223
        assert!(v.is_err());
224
    }
225
}