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
//! Module that defines the at command parser

use crate::tuple_concat::TupleConcat;

/// ```
/// use at_commands::parser::CommandParser;
/// let (x, y, z) = CommandParser::parse(b"+SYSGPIOREAD:654,\"true\",-65154\r\nOK\r\n")
///    .expect_identifier(b"+SYSGPIOREAD:")
///    .expect_int_parameter()
///    .expect_string_parameter()
///    .expect_int_parameter()
///    .expect_identifier(b"\r\nOK\r\n")
///    .finish()
///    .unwrap();
///
/// assert_eq!(x, 654);
/// assert_eq!(y, "true");
/// assert_eq!(z, -65154);
///
/// let (w,) = CommandParser::parse(b"+STATUS: READY\r\nOK\r\n")
///    .expect_identifier(b"+STATUS: ")
///    .expect_raw_string()
///    .expect_identifier(b"\r\nOK\r\n")
///    .finish()
///    .unwrap();
///
/// assert_eq!(w, "READY");
/// ```
#[must_use]
pub struct CommandParser<'a, D> {
    buffer: &'a [u8],
    buffer_index: usize,
    data_valid: bool,
    data: D,
}

impl<'a> CommandParser<'a, ()> {
    /// Start parsing the command
    pub fn parse(buffer: &'a [u8]) -> CommandParser<'a, ()> {
        CommandParser {
            buffer,
            buffer_index: 0,
            data_valid: true,
            data: (),
        }
    }
}
impl<'a, D> CommandParser<'a, D> {
    /// Tries reading an identifier
    pub fn expect_identifier(mut self, identifier: &[u8]) -> Self {
        // If we're already not valid, then quit
        if !self.data_valid {
            return self;
        }

        if self.buffer[self.buffer_index..].len() < identifier.len() {
            self.data_valid = false;
            return self;
        }

        // Zip together the identifier and the buffer data. If all bytes are the same, the data is valid.
        self.data_valid = self.buffer[self.buffer_index..]
            .iter()
            .zip(identifier)
            .all(|(buffer, id)| *buffer == *id);
        // Advance the index
        self.buffer_index += identifier.len();

        self.trim_space()
    }

    /// Moves the internal buffer index over the next bit of space characters, if any
    fn trim_space(mut self) -> Self {
        // If we're already not valid, then quit
        if !self.data_valid {
            return self;
        }

        while let Some(c) = self.buffer.get(self.buffer_index) {
            if *c == b' ' {
                self.buffer_index += 1;
            } else {
                break;
            }
        }

        self
    }

    /// Finds the index of the character after the int parameter or the end of the data.
    fn find_end_of_int_parameter(&mut self) -> usize {
        self.buffer_index
            + self
                .buffer
                .get(self.buffer_index..)
                .map(|buffer| {
                    buffer
                        .iter()
                        .take_while(|byte| {
                            byte.is_ascii_digit() || **byte == b'-' || **byte == b'+'
                        })
                        .count()
                })
                .unwrap_or(self.buffer.len())
    }

    /// Finds the index of the character after the string parameter or the end of the data.
    fn find_end_of_string_parameter(&mut self) -> usize {
        let mut counted_quotes = 0;

        self.buffer_index
            + self
                .buffer
                .get(self.buffer_index..)
                .map(|buffer| {
                    buffer
                        .iter()
                        .take_while(|byte| {
                            counted_quotes += (**byte == b'"') as u8;
                            counted_quotes < 2
                        })
                        .count()
                        + 1
                })
                .unwrap_or(self.buffer.len())
    }

    /// Finds the index of the control character after the non-quoted string or the end of the data.
    fn find_end_of_raw_string(&mut self) -> usize {
        self.buffer_index
            + self
                .buffer
                .get(self.buffer_index..)
                .map(|buffer| {
                    buffer
                        .iter()
                        .take_while(|byte| !(**byte as char).is_ascii_control())
                        .count()
                        + 1
                })
                .unwrap_or(self.buffer.len())
    }

    /// Finish parsing the command and get the results
    pub fn finish(self) -> Result<D, ParseError> {
        if self.data_valid {
            Ok(self.data)
        } else {
            Err(ParseError(self.buffer_index))
        }
    }
}

impl<'a, D: TupleConcat<i32>> CommandParser<'a, D> {
    /// Tries reading an int parameter
    pub fn expect_int_parameter(mut self) -> CommandParser<'a, D::Out> {
        // If we're already not valid, then quit
        if !self.data_valid {
            return CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(0),
            };
        }

        // Get the end index of the current parameter.
        let parameter_end = self.find_end_of_int_parameter();
        // Get the bytes in which the int should reside.
        let int_slice = match self.buffer.get(self.buffer_index..parameter_end) {
            None => {
                self.data_valid = false;
                return CommandParser {
                    buffer: self.buffer,
                    buffer_index: self.buffer_index,
                    data_valid: self.data_valid,
                    data: self.data.tup_cat(0),
                };
            }
            Some(int_slice) => int_slice,
        };
        if int_slice.is_empty() {
            // We probably hit the end of the buffer.
            // The parameter is empty so it is always invalid.
            self.data_valid = false;
            return CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(0),
            };
        }

        // Skip the leading '+'
        let int_slice = if int_slice[0] == b'+' {
            &int_slice[1..]
        } else {
            int_slice
        };

        // Parse the int
        let parsed_int = crate::formatter::parse_int(int_slice);

        // Advance the index to the character after the parameter separator (comma) if it's there.
        self.buffer_index =
            parameter_end + (self.buffer.get(parameter_end) == Some(&b',')) as usize;
        // If we've found an int, then the data may be valid and we allow the closure to set the result ok data.
        if let Some(parameter_value) = parsed_int {
            CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(parameter_value),
            }
        } else {
            self.data_valid = false;
            CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(0),
            }
        }
        .trim_space()
    }
}
impl<'a, D: TupleConcat<&'a str>> CommandParser<'a, D> {
    /// Tries reading a string parameter
    pub fn expect_string_parameter(mut self) -> CommandParser<'a, D::Out> {
        // If we're already not valid, then quit
        if !self.data_valid {
            return CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(""),
            };
        }

        // Get the end index of the current parameter.
        let parameter_end = self.find_end_of_string_parameter();
        if parameter_end > self.buffer.len() {
            // We hit the end of the buffer.
            // The parameter is empty so it is always invalid.
            self.data_valid = false;
            return CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(""),
            };
        }
        // Get the bytes in which the string should reside.
        let string_slice = &self.buffer[(self.buffer_index + 1)..(parameter_end - 1)];

        let has_comma_after_parameter = if let Some(next_char) = self.buffer.get(parameter_end) {
            *next_char == b','
        } else {
            false
        };

        // Advance the index to the character after the parameter separator.
        self.buffer_index = parameter_end + has_comma_after_parameter as usize;
        // If we've found a valid string, then the data may be valid and we allow the closure to set the result ok data.
        if let Ok(parameter_value) = core::str::from_utf8(string_slice) {
            CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(parameter_value),
            }
        } else {
            self.data_valid = false;
            CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(""),
            }
        }
        .trim_space()
    }

    /// Tries reading a non-parameter, non-quoted string
    pub fn expect_raw_string(mut self) -> CommandParser<'a, D::Out> {
        // If we're already not valid, then quit
        if !self.data_valid {
            return CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(""),
            };
        }

        // Get the end index of the current string.
        let end = self.find_end_of_raw_string();
        // Get the bytes in which the string should reside.
        let string_slice = &self.buffer[self.buffer_index..(end - 1)];

        // Advance the index to the character after the string.
        self.buffer_index = end - 1usize;

        // If we've found a valid string, then the data may be valid and we allow the closure to set the result ok data.
        if let Ok(parameter_value) = core::str::from_utf8(string_slice) {
            CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(parameter_value),
            }
        } else {
            self.data_valid = false;
            CommandParser {
                buffer: self.buffer,
                buffer_index: self.buffer_index,
                data_valid: self.data_valid,
                data: self.data.tup_cat(""),
            }
        }
        .trim_space()
    }
}

/// Error type for parsing
///
/// The number is the index of up to where it was correctly parsed
#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ParseError(usize);

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

    #[test]
    fn test_ok() {
        let (x, y, z) = CommandParser::parse(b"+SYSGPIOREAD:654,\"true\",-65154\r\nOK\r\n")
            .expect_identifier(b"+SYSGPIOREAD:")
            .expect_int_parameter()
            .expect_string_parameter()
            .expect_int_parameter()
            .expect_identifier(b"\r\nOK\r\n")
            .finish()
            .unwrap();

        assert_eq!(x, 654);
        assert_eq!(y, "true");
        assert_eq!(z, -65154);
    }

    #[test]
    fn test_positive_int_param() {
        let (x,) = CommandParser::parse(b"OK+RP:+20dBm\r\n")
            .expect_identifier(b"OK+RP:")
            .expect_int_parameter()
            .expect_identifier(b"dBm\r\n")
            .finish()
            .unwrap();

        assert_eq!(x, 20);
    }

    #[test]
    fn test_whitespace() {
        let (x, y, z) = CommandParser::parse(b"+SYSGPIOREAD: 654, \"true\", -65154 \r\nOK\r\n")
            .expect_identifier(b"+SYSGPIOREAD:")
            .expect_int_parameter()
            .expect_string_parameter()
            .expect_int_parameter()
            .expect_identifier(b"\r\nOK\r\n")
            .finish()
            .unwrap();

        assert_eq!(x, 654);
        assert_eq!(y, "true");
        assert_eq!(z, -65154);
    }

    #[test]
    fn string_param_at_end() {
        let (x, y) = CommandParser::parse(br#"+SYSGPIOREAD: 42, "param at end""#)
            .expect_identifier(b"+SYSGPIOREAD:")
            .expect_int_parameter()
            .expect_string_parameter()
            .finish()
            .unwrap();

        assert_eq!(x, 42);
        assert_eq!(y, "param at end");
    }
}