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
|
use mon::{Parser, ParserIter, ascii_alpha, ascii_alphanumeric, r#if, one_of, tag};
use crate::{
Parseable,
repo::profile::make_defaults::{Assignment, Interpolation, Key, Literal, Segment},
};
impl<'a> Parseable<'a, &'a str> for Key {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
let start = ascii_alpha();
let rest = ascii_alphanumeric()
.or(one_of("_".chars()))
.repeated()
.many();
start
.followed_by(rest)
.recognize()
.map(|output: &str| Key(output.to_string()))
}
}
impl<'a> Parseable<'a, &'a str> for Literal {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
r#if(|c: &char| *c != '"')
.and_not(Interpolation::parser())
.repeated()
.at_least(1)
.recognize()
.map(|output: &str| Literal(output.to_string()))
}
}
impl<'a> Parseable<'a, &'a str> for Interpolation {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
Key::parser()
.recognize()
.delimited_by(tag("{"), tag("}"))
.preceded_by(tag("$"))
.map(|output: &str| Interpolation(output.to_string()))
}
}
impl<'a> Parseable<'a, &'a str> for Segment {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
Literal::parser()
.map(Segment::Literal)
.or(Interpolation::parser().map(Segment::Interpolation))
}
}
impl<'a> Parseable<'a, &'a str> for Assignment {
type Parser = impl Parser<&'a str, Output = Self>;
fn parser() -> Self::Parser {
Key::parser()
.followed_by(tag("="))
.and(
Segment::parser()
.repeated()
.many()
.delimited_by(tag("\""), tag("\"")),
)
.map(|(key, value)| Assignment(key, value))
}
}
#[cfg(test)]
mod test {
use mon::input::InputIter;
use super::*;
#[test]
fn test_parse_value() {
let it = InputIter::new(r#"KEY="foo ${bar}""#);
Assignment::parser().check_finished(it).unwrap();
}
}
|