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
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
Word(String),
String(String),
Variable(String),
Newline,
Body(String),
End,
}
#[derive(Debug)]
pub enum LexerError {
UnterminatedString,
InvalidVariable,
}
impl fmt::Display for LexerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LexerError::UnterminatedString => {
write!(f, "unterminated string: missing closing quote")
}
LexerError::InvalidVariable => {
write!(f, "invalid variable: expected format {{name}}")
}
}
}
}
pub struct Lexer;
impl Lexer {
pub fn lex(input: &str) -> Result<Vec<Token>, LexerError> {
let mut tokens = Vec::new();
let mut lines = input.lines().peekable();
while let Some(line) = lines.next() {
// Empty line means body starts
if line.trim().is_empty() {
let mut body = Vec::new();
for rest in lines.by_ref() {
if rest.trim() == "###" {
break;
}
body.push(rest);
}
let body = body.join("\n");
if !body.is_empty() {
tokens.push(Token::Body(body));
}
break;
}
// Request terminator
if line.trim() == "###" {
continue;
}
tokens.extend(Self::lex_line(line)?);
tokens.push(Token::Newline);
}
tokens.push(Token::End);
Ok(tokens)
}
fn lex_line(line: &str) -> Result<Vec<Token>, LexerError> {
let mut tokens = Vec::new();
let chars: Vec<char> = line.chars().collect();
let mut current = String::new();
let mut i = 0;
while i < chars.len() {
match chars[i] {
// Quoted string
'"' | '\'' => {
if !current.is_empty() {
tokens.push(Token::Word(current.clone()));
current.clear();
}
let quote = chars[i];
i += 1;
let mut value = String::new();
while i < chars.len() {
let c = chars[i];
// Closing quote
if c == quote {
break;
}
// Escape handling
if c == '\\' {
i += 1;
if i >= chars.len() {
return Err(LexerError::UnterminatedString);
}
let escaped = match chars[i] {
'n' => '\n',
't' => '\t',
'\\' => '\\',
'"' => '"',
'\'' => '\'',
other => other,
};
value.push(escaped);
i += 1;
continue;
}
value.push(c);
i += 1;
}
if i >= chars.len() {
return Err(LexerError::UnterminatedString);
}
tokens.push(Token::String(value));
i += 1;
}
// Variable {{name}}
'{' if i + 1 < chars.len() && chars[i + 1] == '{' => {
if !current.is_empty() {
tokens.push(Token::Word(current.clone()));
current.clear();
}
i += 2;
let mut name = String::new();
while i + 1 < chars.len() && !(chars[i] == '}' && chars[i + 1] == '}') {
name.push(chars[i]);
i += 1;
}
if i + 1 >= chars.len() {
return Err(LexerError::InvalidVariable);
}
let name = name.trim();
// Validate variable name
if name.is_empty()
|| !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
{
return Err(LexerError::InvalidVariable);
}
tokens.push(Token::Variable(name.to_string()));
i += 2;
}
// Whitespace ends word
' ' | '\t' => {
if !current.is_empty() {
tokens.push(Token::Word(current.clone()));
current.clear();
}
i += 1;
}
// Everything else is part of word
c => {
current.push(c);
i += 1;
}
}
}
if !current.is_empty() {
tokens.push(Token::Word(current));
}
Ok(tokens)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn tokens(input: &str) -> Vec<Token> {
Lexer::lex(input).unwrap()
}
fn body(input: &str) -> Option<String> {
tokens(input).into_iter().find_map(|token| match token {
Token::Body(body) => Some(body),
_ => None,
})
}
#[test]
fn terminator_line_is_dropped() {
let tokens = tokens("GET /users\npage=1\n###");
assert!(
!tokens
.iter()
.any(|token| matches!(token, Token::Word(word) if word == "###"))
);
}
#[test]
fn body_stops_at_terminator() {
assert_eq!(
body("POST /api\n\nhello world\n###"),
Some("hello world".to_string())
);
}
#[test]
fn body_without_terminator_keeps_all_lines() {
assert_eq!(
body("POST /api\n\nhello\nworld"),
Some("hello\nworld".to_string())
);
}
}