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
use crate::string_cache::StringCache;
use crate::token::{build_phrase, Token};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rule {
    pub id: i32,
    pub inputs: Vec<Vec<Token>>,
    pub outputs: Vec<Vec<Token>>,
}

impl Rule {
    pub fn new(id: i32, inputs: Vec<Vec<Token>>, outputs: Vec<Vec<Token>>) -> Rule {
        Rule {
            id,
            inputs,
            outputs,
        }
    }

    pub fn to_string(&self, string_cache: &StringCache) -> String {
        let inputs = self
            .inputs
            .iter()
            .map(|p| build_phrase(p, string_cache))
            .collect::<Vec<_>>()
            .join(" . ");

        let outputs = self
            .outputs
            .iter()
            .map(|p| build_phrase(p, string_cache))
            .collect::<Vec<_>>()
            .join(" . ");

        format!("{:5}: {} = {}", self.id, inputs, outputs)
    }
}