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
#![deny(clippy::all)]

#[macro_use]
extern crate pest_derive;

#[cfg(test)]
#[macro_use]
extern crate parameterized;

use pest::Parser;

use crate::errors::SicParserError;
use crate::rule_parser::parse_image_operations;
use naut_image_engine::engine::Instr;

pub mod errors;
pub mod named_value;
pub mod rule_parser;
pub mod value_parser;

const PARSER_RULE: Rule = Rule::main;

#[derive(Parser)]
#[grammar = "grammar.pest"]
pub struct SICParser;

pub fn parse_script(script: &str) -> Result<Vec<Instr>, SicParserError> {
    let parsed_script = SICParser::parse(PARSER_RULE, script);

    parsed_script
        .map_err(|err| SicParserError::PestGrammarError(err.to_string()))
        .and_then(parse_image_operations)
}

#[cfg(test)]
mod tests {
    use naut_image_engine::engine::Instr;
    use naut_image_engine::ImgOp;

    use super::*;

    #[test]
    fn test_too_many_args() {
        let input = "blur 15 28;";
        let parsed = SICParser::parse(Rule::main, input);

        // Manually constructing a pest::Error::ParsingError is a hell, because of Position, which,
        // except for the from_start() method is private.
        assert!(parsed.is_err());
    }

    #[test]
    fn test_parsed() {
        let input = "blur 15; flip-vertical";
        let parsed = parse_script(input);

        assert!(parsed.is_ok());

        assert_eq!(
            parsed.unwrap(),
            vec![
                Instr::Operation(ImgOp::Blur(15.0)),
                Instr::Operation(ImgOp::FlipVertical)
            ]
        );
    }

    #[test]
    fn test_parsed_fail() {
        let input = "blur 15.7.; flipv";
        let parsed = parse_script(input);

        assert!(parsed.is_err());
    }
}