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
#![recursion_limit = "1024"]

#[macro_use]
extern crate error_chain;
extern crate pom;

#[macro_use]
mod macros;

pub mod lexer;

use pom::DataInput;

mod errors {
    // Create the Error, ErrorKind, ResultExt, and Result types
    error_chain! {

    errors {
        NoCommand {}
        NotEnoughArguments {}
    }

    }

}

use errors::*;

#[derive(Debug, PartialEq)]
pub struct Command {
    name: String,
    id: Option<u8>,
    args: Vec<String>,
}

pub fn try_parse(input: &[u8]) -> Result<Command> {
    let input = DataInput::new(input);
    let parse = lexer::command().parse(&mut input.clone());
    let (id, mut parse) = parse.unwrap();

    let rest = parse.split_off(1);
    let ref first = parse[0];
    Ok(Command { name: first.clone(), id: id, args: rest })
}

#[test]
fn parse_quit() {
    let quit = try_parse(b"quit");
    assert_eq!(quit.unwrap(), Command{ name: "quit".to_string(), id: None, args: Vec::new() });
}