command

Function command 

Source
pub fn command(input: &[u8]) -> IResult<&[u8], Command>
Examples found in repository?
examples/parse_command.rs (line 11)
5fn main() -> std::io::Result<()> {
6    let mut args = std::env::args();
7
8    if let Some(path) = args.nth(1) {
9        let data = std::fs::read(path).unwrap();
10
11        match command(&data) {
12            Ok((remaining, command)) => {
13                println!("[!] {:#?}", command);
14                let serialized = {
15                    let mut serialized = Vec::new();
16                    command.serialize(&mut serialized).unwrap();
17                    String::from_utf8(serialized).unwrap()
18                };
19                print!("[!] {}", serialized);
20
21                if !remaining.is_empty() {
22                    println!("Remaining data in buffer: {:?}", remaining);
23                }
24            }
25            Err(error) => {
26                println!("Error parsing the command. Is it correct? ({:?})", error);
27            }
28        }
29
30        return Ok(());
31    }
32
33    loop {
34        let line = {
35            print!("Enter SMTP command (or \"exit\"): ");
36            std::io::stdout().flush().unwrap();
37
38            let mut line = String::new();
39            std::io::stdin().read_line(&mut line)?;
40            line.replace("\n", "\r\n")
41        };
42
43        if line.trim() == "exit" {
44            break;
45        }
46
47        match command(line.as_bytes()) {
48            Ok((remaining, command)) => {
49                println!("[!] {:#?}", command);
50                let serialized = {
51                    let mut serialized = Vec::new();
52                    command.serialize(&mut serialized).unwrap();
53                    String::from_utf8(serialized).unwrap()
54                };
55                print!("[!] {}", serialized);
56
57                if !remaining.is_empty() {
58                    println!("Remaining data in buffer: {:?}", remaining);
59                }
60            }
61            Err(error) => {
62                println!("Error parsing the command. Is it correct? ({:?})", error);
63            }
64        }
65    }
66
67    Ok(())
68}