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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! # scfg-rs
//! A rust library for parsing [scfg] files. Scfg is a simple line oriented
//! configuration file format. Every line may contain at most one directive per
//! line. A directive consists of a name, followed by optional parameters
//! separated by whitespace followed by an optional child block, delimited by
//! `{` and `}`. Whitespace at the beginning of lines is insignificant. Lines
//! beginning with `#` are comments and are ignored.
//!
//! [scfg]: https://git.sr.ht/~emersion/scfg
//!
//! ## Examples
//! ```
//! # use scfg::*;
//! // an scfg document
//! static SCFG_DOC: &str = r#"train "Shinkansen" {
//!     model "E5" {
//!         max-speed 320km/h
//!         weight 453.5t
//!
//!         lines-served "Tōhoku" "Hokkaido"
//!     }
//!
//!     model "E7" {
//!         max-speed 275km/h
//!         weight 540t
//!
//!         lines-served "Hokuriku" "Jōetsu"
//!     }
//! }"#;
//! let doc = SCFG_DOC.parse::<Scfg>().expect("invalid document");
//!
//! // the above document can also be created with this builder style api
//! let mut scfg = Scfg::new();
//! let train = scfg
//!     .add("train")
//!     .append_param("Shinkansen")
//!     .get_or_create_child();
//! let e5 = train.add("model").append_param("E5").get_or_create_child();
//! e5.add("max-speed").append_param("320km/h");
//! e5.add("weight").append_param("453.5t");
//! e5.add("lines-served")
//!     .append_param("Tōhoku")
//!     .append_param("Hokkaido");
//! let e7 = train.add("model").append_param("E7").get_or_create_child();
//! e7.add("max-speed").append_param("275km/h");
//! e7.add("weight").append_param("540t");
//! e7.add("lines-served")
//!     .append_param("Hokuriku")
//!     .append_param("Jōetsu");
//!
//! assert_eq!(doc, scfg);
//! ```
use std::{borrow::Borrow, hash::Hash, io, str::FromStr};

#[cfg(feature = "preserve_order")]
use indexmap::IndexMap;
#[cfg(not(feature = "preserve_order"))]
use std::collections::BTreeMap;

pub type ParseError = peg::error::ParseError<peg::str::LineCol>;

/// An scfg document. Implemented as a multimap.
///
/// If the `preserve_order` feature is enabled, the directive names will be kept
/// in the order of their first appearance.  Otherwise, they will be sorted by name.
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Scfg {
    directives: Map<String, Vec<Directive>>,
}

#[cfg(not(feature = "preserve_order"))]
type Map<K, V> = BTreeMap<K, V>;
#[cfg(feature = "preserve_order")]
type Map<K, V> = IndexMap<K, V>;

impl Scfg {
    /// Creates a new empty document
    pub fn new() -> Self {
        Default::default()
    }

    /// Retrieves the first directive with a particular name.
    ///
    /// This will return `None` if either, the name is not found, or if the name
    /// somehow has no directives.
    pub fn get<Q>(&self, name: &Q) -> Option<&Directive>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash + ?Sized,
    {
        self.directives.get(name).and_then(|d| d.first())
    }

    /// Retrieves the all directives with a particular name.
    pub fn get_all<Q>(&self, name: &Q) -> Option<&[Directive]>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash + ?Sized,
    {
        self.directives.get(name).map(|ds| ds.as_ref())
    }

    /// Retrieves a mutable reference to all directives with a particular name.
    pub fn get_all_mut<Q>(&mut self, name: &Q) -> Option<&mut Vec<Directive>>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash + ?Sized,
    {
        self.directives.get_mut(name)
    }

    /// Does the document contain a directive with `name`.
    ///
    /// ```
    /// # use scfg::*;
    /// let mut scfg = Scfg::new();
    /// scfg.add("foo");
    /// assert!(scfg.contains("foo"));
    /// assert!(!scfg.contains("bar"));
    /// ```
    pub fn contains<Q>(&self, name: &Q) -> bool
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash + ?Sized,
    {
        self.directives.contains_key(name)
    }

    /// Adds a new name returning the new (empty) directive.
    /// ```
    /// # use scfg::*;
    /// let mut scfg = Scfg::new();
    /// let dir = scfg.add("dir1");
    /// assert_eq!(*dir, Directive::default());
    /// ```
    ///
    /// # Note
    /// This does not validate that `name` is a legal scfg word. It is possible to create
    /// unparsable documents should `name` contain control characters or newlines.
    pub fn add(&mut self, name: impl Into<String>) -> &mut Directive {
        let entry = self.directives.entry(name.into()).or_insert_with(Vec::new);
        entry.push(Directive::default());
        entry.last_mut().unwrap()
    }

    /// Removes all directives with the supplied name, returning them.
    pub fn remove<Q>(&mut self, name: &Q) -> Option<Vec<Directive>>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash + ?Sized,
    {
        self.directives.remove(name)
    }

    /// Removes all directives with the supplied name, returning them, and their
    /// key.
    pub fn remove_entry<Q>(&mut self, name: &Q) -> Option<(String, Vec<Directive>)>
    where
        String: Borrow<Q>,
        Q: Ord + Eq + Hash + ?Sized,
    {
        self.directives.remove_entry(name)
    }

    /// Writes the document to the specified writer. If efficiency is a concern,
    /// it may be best to wrap the writer in a [`BufWriter`] first. This will
    /// not write any comments that the document had if it was parsed first.
    pub fn write<W>(&self, writer: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        self.write_with_indent(0, writer)
    }

    fn write_with_indent<W>(&self, indent: usize, wtr: &mut W) -> io::Result<()>
    where
        W: io::Write,
    {
        let mut prefix = "";
        for (name, directives) in &self.directives {
            for directive in directives {
                wtr.write_all(prefix.as_ref())?;
                prefix = "";
                for _ in 0..indent {
                    write!(wtr, "\t")?;
                }
                write!(wtr, "{}", shell_words::quote(&name))?;
                for param in &directive.params {
                    write!(wtr, " {}", shell_words::quote(&param))?;
                }

                if let Some(ref child) = directive.child {
                    wtr.write_all(b" {\n")?;
                    child.write_with_indent(indent + 1, wtr)?;
                    for _ in 0..indent {
                        wtr.write_all(b"\t")?;
                    }
                    wtr.write_all(b"}")?;
                    prefix = "\n";
                }
                wtr.write_all(b"\n")?;
            }
        }

        Ok(())
    }
}

impl FromStr for Scfg {
    type Err = ParseError;
    fn from_str(src: &str) -> Result<Self, Self::Err> {
        scfg_parser::document(src)
    }
}

impl<K: Into<String>> std::iter::FromIterator<(K, Directive)> for Scfg {
    fn from_iter<T>(it: T) -> Self
    where
        T: IntoIterator<Item = (K, Directive)>,
    {
        let mut scfg = Self::default();

        for (name, directive) in it {
            let name = name.into();
            scfg.directives
                .entry(name)
                .or_insert_with(Vec::new)
                .push(directive);
        }

        scfg
    }
}

/// A single scfg directive, containing any number of parameters, and possibly
/// one child block.
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Directive {
    params: Vec<String>,
    child: Option<Scfg>,
}

impl Directive {
    /// Creates a new empty directive.
    pub fn new() -> Self {
        Default::default()
    }

    /// Get this directive's parameters
    pub fn params(&self) -> &[String] {
        &self.params
    }

    /// Appends the supplied parameter. Returns `&mut self` to support method
    /// chaining.
    ///
    /// # Note
    /// This does not validate that `param` is a legal scfg word. It is possible to create
    /// unparsable documents should `param` contain control characters or newlines.
    pub fn append_param(&mut self, param: impl Into<String>) -> &mut Self {
        self.params.push(param.into());
        self
    }

    /// Clears all parameters from this directive.
    pub fn clear_params(&mut self) {
        self.params.clear();
    }

    /// Get this directive's child, if there is one.
    pub fn child(&self) -> Option<&Scfg> {
        self.child.as_ref()
    }

    /// Takes this directive's child, leaving it with `None`.
    pub fn take_child(&mut self) -> Option<Scfg> {
        self.child.take()
    }

    /// Returns the child, optionally creating it if it does not exist.
    ///
    /// ```
    /// # use scfg::*;
    /// let mut directive = Directive::new();
    /// assert!(directive.child().is_none());
    /// directive.get_or_create_child();
    /// assert!(directive.child().is_some());
    /// ```
    pub fn get_or_create_child(&mut self) -> &mut Scfg {
        self.child.get_or_insert_with(Scfg::new)
    }
}

peg::parser! {
    grammar scfg_parser() for str {
        pub(crate) rule document() -> Scfg =
            blank() _ directives:directive()*
        {
            directives.into_iter().collect()
        }

        rule directive() -> (String, Directive) =
            blank() _ name:word() _ params:params() _ child:block()? newline()
        {
            (name, Directive { params, child })
        }

        rule block() -> Scfg =
            "{" newline() document:document() "}"
        {
            document
        }

        rule word() -> String =
            val:(atom() / dquote() / squote())
        {
            val
        }

        rule params() -> Vec<String> = vals:word() ** _ { vals }

        rule atom() -> String =
            s:$(quiet!{(['\x21'|'\x23'..='\x26'|'\x28'..='\x5b'|'\x5d'..='\x7a'
                 |'\x7c'|'\x7e'|'\u{80}'..='\u{10FFFF}'] / esc_pair())+} /
                expected!("an atom"))
        {
            if s.contains('\\') {
                s.chars().filter(|&c| c != '\\').collect::<String>()
            } else {
                String::from(s)
            }
        }

        rule dquote() -> String =
            s:$(quiet!{"\"" (['\t'|'\x20'|'\x21'|'\x23'..='\x5b'|'\x5d'..='\x7e'
                      |'\u{80}'..='\u{10FFFF}'] / esc_pair())+"\""}
                / expected!("a double quoted string"))
        {
            let s = &s[1..s.len() - 1];
            if s.contains('\\') {
                s.chars().filter(|&c| c != '\\').collect::<String>()
            } else {
                String::from(s)
            }
        }

        rule squote() -> String =
            s:$(quiet!{"'" ['\t'|'\x20'..='\x26'|'\x28'..='\x7e'|'\u{80}'..='\u{10FFFF}']+ "'"}
                / expected!("a single quoted string"))
        {
            s[1..s.len()-1].to_owned()
        }

        rule esc_pair() = ['\\']['\t'|'\x20'..='\x7e'|'\u{80}'..='\u{10FFFF}']

        rule blank() = quiet!{([' '|'\t']* ("#" [c if c != '\n']*)? ['\n'])*}
        rule _() = quiet!{[' '|'\t']*}
        rule newline() = quiet!{(_ "\n")+ _ / ![_]}
        rule __() = newline()*
    }
}

#[cfg(test)]
mod test {
    use super::*;

    type Result = std::result::Result<(), Box<dyn std::error::Error>>;

    #[test]
    fn flat() -> Result {
        let src = r#"dir1 param1 param2 param3
dir2
dir3 param1

# comment
dir4 "param 1" 'param 2'
"#;
        let cfg = Scfg::from_str(src)?;
        // this tests the fromiter impl
        // builder type api is generally a little cleaner
        let exp = vec![
            (
                "dir1",
                Directive {
                    params: vec!["param1".into(), "param2".into(), "param3".into()],
                    child: None,
                },
            ),
            (
                "dir2",
                Directive {
                    params: vec![],
                    child: None,
                },
            ),
            (
                "dir3",
                Directive {
                    params: vec!["param1".into()],
                    child: None,
                },
            ),
            (
                "dir4",
                Directive {
                    params: vec!["param 1".into(), "param 2".into()],
                    child: None,
                },
            ),
        ]
        .into_iter()
        .collect::<Scfg>();
        assert_eq!(cfg, exp);

        Ok(())
    }

    #[test]
    fn simple_blocks() -> Result {
        let src = r#"block1 {
    dir1 param1 param2
    dir2 param1
}

block2 {
}

block3 {
    # comment
}

block4 param1 "param2" {
    dir1
}"#;
        let cfg = Scfg::from_str(src)?;
        let mut exp = Scfg::new();
        let block1 = exp.add("block1");
        let block = block1.get_or_create_child();
        block
            .add("dir1")
            .append_param("param1")
            .append_param("param2");
        block.add("dir2").append_param("param1");
        exp.add("block2").get_or_create_child();
        exp.add("block3").get_or_create_child();
        exp.add("block4")
            .append_param("param1")
            .append_param("param2")
            .get_or_create_child()
            .add("dir1");

        assert_eq!(cfg, exp);
        Ok(())
    }

    #[test]
    fn nested() -> Result {
        let src = r#"block1 {
    block2 {
        dir1 param1
    }

    block3 {
    }
}

block4 {
    block5 {
        block6 param1 {
            dir1
        }
    }

    dir1
}"#;
        let cfg = Scfg::from_str(src)?;
        let mut exp = Scfg::new();
        let block1 = exp.add("block1").get_or_create_child();
        block1
            .add("block2")
            .get_or_create_child()
            .add("dir1")
            .append_param("param1");
        block1.add("block3").get_or_create_child();
        let block4 = exp.add("block4").get_or_create_child();
        block4
            .add("block5")
            .get_or_create_child()
            .add("block6")
            .append_param("param1")
            .get_or_create_child()
            .add("dir1");
        block4.add("dir1");

        assert_eq!(cfg, exp);

        Ok(())
    }

    #[test]
    fn write() -> Result {
        let src = r#"dir1 param1 param2 param3
dir2
dir3 param1

# comment
dir4 "param 1" 'param 2'
"#;
        let doc = Scfg::from_str(src)?;
        let mut out = Vec::new();
        doc.write(&mut out)?;
        let exp = r#"dir1 param1 param2 param3
dir2
dir3 param1
dir4 'param 1' 'param 2'
"#;
        assert_eq!(std::str::from_utf8(&out)?, exp);
        Ok(())
    }

    #[test]
    fn write_block() -> Result {
        let src = r#"block1 {
	dir1 param1 param2
	dir2 param1
}

block2 {
}

block3 {
	# comment
}

block4 param1 "param2" {
	dir1
}"#;
        let doc = Scfg::from_str(src)?;
        let mut out = Vec::new();
        doc.write(&mut out)?;
        let exp = r#"block1 {
	dir1 param1 param2
	dir2 param1
}

block2 {
}

block3 {
}

block4 param1 param2 {
	dir1
}
"#;
        assert_eq!(std::str::from_utf8(&out)?, exp);
        Ok(())
    }
}