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
use crate::pest::Parser;

/// Parser for text that meets the "quoted-string" grammar.
///```rust
/// use quoted_string_parser::{QuotedStringParser, QuotedStringParseLevel};
///
/// // two qdtexts separated by a whitespace
/// assert!(QuotedStringParser::validate(
///   QuotedStringParseLevel::QuotedString, "\"Hello world\""));
///
/// // one quoted-pair
/// assert!(QuotedStringParser::validate(
///   QuotedStringParseLevel::QuotedString, "\"\\\u{7f}\""));
///```
///
/// QuotedStringParser derives from [Parser](https://docs.rs/pest/latest/pest/trait.Parser.html),
/// if you need more control over the parser itself you can use any
/// of the operations defined in the [pest](https://docs.rs/pest/latest/pest/)
/// crate. Check the documentation for more information.
#[derive(Parser)]
#[grammar = "quoted_string.pest"]
pub struct QuotedStringParser;

impl QuotedStringParser {
    /// Validate that the input meets the grammar
    pub fn validate(lvl: QuotedStringParseLevel, input: &str) -> bool {
        let rule = match lvl {
            QuotedStringParseLevel::QuotedString => Rule::quoted_string,
            QuotedStringParseLevel::QuotedText => Rule::quoted_text,
        };
        QuotedStringParser::parse(rule, input).is_ok()
    }
}

/// Defines the level at which the grammar should be applied.
pub enum QuotedStringParseLevel {
    /// The whole quoted-string grammar.
    QuotedString,
    /// Only sequences of qdtext / quoted-pair values. Some protocols
    /// like Stun only checks sequences of qdtext and quoted-pairs
    /// without the double quotes and their surrounding whitespaces.
    QuotedText,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pest::Parser;

    #[test]
    fn valid_quoted_string() {
        // Empty quoted string ""
        QuotedStringParser::parse(Rule::quoted_string, "\"\"")
            .expect("Could not parse QuotedString");

        // qdtext
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{21}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{23}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{5b}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{5d}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{7e}\"")
            .expect("Could not parse QuotedString");

        // qdtext (utf8_non_ascii)
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{c0}\u{80}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{df}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{e0}\u{80}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{ef}\u{80}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f0}\u{80}\u{81}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f7}\u{80}\u{81}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f8}\u{80}\u{81}\u{82}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fb}\u{80}\u{81}\u{82}\u{bf}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(
            Rule::quoted_string,
            "\"\u{fc}\u{80}\u{81}\u{82}\u{83}\u{bf}\"",
        )
        .expect("Could not parse QuotedString");
        QuotedStringParser::parse(
            Rule::quoted_string,
            "\"\u{fd}\u{80}\u{81}\u{82}\u{83}\u{bf}\"",
        )
        .expect("Could not parse QuotedString");

        // quoted-pair
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{00}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{09}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{0b}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{0c}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{0e}\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{7f}\"")
            .expect("Could not parse QuotedString");

        // quoted-string with series of qdtext and quoted-pair
        QuotedStringParser::parse(Rule::quoted_string, "\"\\abcdfg\\h\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(
            Rule::quoted_string,
            "\"\\abfg\\h\u{fd}\u{80}\u{81}\u{82}\u{83}\u{bf}\"",
        )
        .expect("Could not parse QuotedString");

        // quoted string with CRLF and whitespaces
        QuotedStringParser::parse(Rule::quoted_string, "\"hello world\"")
            .expect("Could not parse QuotedString");
        QuotedStringParser::parse(Rule::quoted_string, "\" \u{0d}\u{0a}   hello\"")
            .expect("Could not parse QuotedString");
    }

    #[test]
    fn invalid_quoted_string() {
        // qdtext (miss one ut8 cont. character)
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{c0}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{e0}\u{80}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f7}\u{80}\u{81}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f8}\u{80}\u{81}\u{82}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fd}\u{80}\u{81}\u{82}\u{83}\"")
            .expect_err("Parse should have failed");

        // qdtext (miss two ut8 cont. character)
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{e0}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f7}\u{80}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f8}\u{80}\u{81}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fd}\u{80}\u{81}\u{82}\"")
            .expect_err("Parse should have failed");

        // qdtext (miss three ut8 cont. character)
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f7}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{f8}\u{80}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fd}\u{80}\u{81}\"")
            .expect_err("Parse should have failed");

        // qdtext (miss four ut8 cont. character)
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fb}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fd}\u{80}\"")
            .expect_err("Parse should have failed");

        // qdtext (miss five ut8 cont. character)
        QuotedStringParser::parse(Rule::quoted_string, "\"\u{fd}\"")
            .expect_err("Parse should have failed");

        // invalid quoted-string
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{0a}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{0d}\"")
            .expect_err("Parse should have failed");
        QuotedStringParser::parse(Rule::quoted_string, "\"\\\u{8a}\"")
            .expect_err("Parse should have failed");

        // lws failed with missinf LF character
        QuotedStringParser::parse(Rule::quoted_string, "\" \u{0d} hello\"")
            .expect_err("Parse should have failed");
    }
}