aboutsummaryrefslogtreecommitdiff
path: root/src/plumber_core/rule_engine/parse.rs
blob: 5c2739d82204309d0a8244381040150c9e04dc0e (plain)
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
// temporary
#![allow(dead_code)]

use std::vec::Vec;

pub struct Parser {
    input: String,
    line: i32,
}

pub struct Var {
    name: String,
    value: String,
}

const OBJECTS: [&str; 8] = ["src", "dst", "wdir", "type", "attr", "ndata", "data", "arg"];
const ACTION: [&str; 1] = ["plumb"];
const VERBS: [&str; 5] = ["add", "is", "isdir", "isfile", "set"];

impl Parser {
    pub fn new(input: String) -> Parser {
        Self { input, line: 0 }
    }

    pub fn parse(self: &Self) {
        let rules = self.collect_rules();
        println!("{rules:#?}")
    }

    fn collect_rules(self: &Self) -> Vec<Vec<String>> {
        let mut rules: Vec<Vec<String>> = vec![];
        let mut current_rule: Vec<String> = vec![];
        let mut lines = self.input.lines().peekable();

        while lines.peek().is_some() {
            // clean line from whitespace characters, testing file includes many
            let line: String = lines
                .next()
                .unwrap()
                .to_string()
                .chars()
                .map(|c| if c.is_whitespace() { ' ' } else { c })
                .collect();

            if !line.starts_with("#") && !line.trim().is_empty() {
                current_rule.push(line);
            } else if !current_rule.is_empty() {
                rules.push(current_rule);
                current_rule = vec![];
            }
        }
        rules
    }
}