light_cli/
input.rs

1use heapless;
2use nb;
3use tokenizer;
4
5use tokenizer::{Tokenizer};
6use lexer::{Lexer, CallbackCommand};
7use hal::serial::Read;
8
9pub struct LightCliInput<SLEN> where SLEN: heapless::ArrayLength<u8> {
10    tokenizer: Tokenizer<SLEN>,
11    lexer: Lexer<SLEN>
12}
13
14impl<SLEN : heapless::ArrayLength<u8>> LightCliInput<SLEN> {
15    /// Create a new LightCLI instance.
16    pub fn new() -> Self {
17        Self {
18            tokenizer: Tokenizer::new(),
19            lexer: Lexer::new(),
20        }
21    }
22
23    /// Try to parse as much data from the internal ring buffer as possible.
24    /// 
25    /// # Arguments
26    /// * `callback` - This is the callback that will receive all parsing events.
27    /// 
28    /// # Remarks
29    /// All commands are in the form "COMMAND KEY=VALUE". For every parsed key/value 
30    /// pair the callback will be triggered with the current command string, current
31    /// key and the corresponding value. When a newline is read the callback is 
32    /// triggered with a command event.
33    pub fn parse_data<CB>(&mut self, callback: CB) -> nb::Result<(), tokenizer::Error> 
34        where CB: FnMut(CallbackCommand) -> () {
35        self.lexer.parse_data(&mut self.tokenizer, callback)
36    }
37
38    /// Copy as many available bytes from `ser` into the buffer as possible.
39    /// 
40    /// # Arguments
41    /// * `ser` - The serial interface to read from.
42    /// 
43    /// # Remarks
44    /// 
45    /// This will continue to try to read a byte from the serial device until the
46    /// device returns `nb::Error::WouldBlock`.
47    pub fn fill<E>(&mut self, ser: &mut Read<u8, Error=E>) -> nb::Result<(), E> {
48        self.tokenizer.fill(ser)
49    }
50}