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
use std::collections::HashMap;
use std::fmt;

/// An individual element in an ABNF rule.
#[derive(PartialEq, Clone)]
pub enum Element {
    /// rulename.
    Rulename(String),
    /// case insensitive string.
    IString(String),
    /// case seisitve string.
    SString(String),
    /// num-val.
    NumberValue(u32),
    /// range of num-val.
    ValueRange((u32, u32)),
    /// sequence of num-val.
    ValueSequence(Vec<u32>),
    /// prose-val.
    ProseValue(String),
    /// concatination.
    Sequence(Vec<Repetition>),
    /// alternation.
    Selection(Vec<Repetition>),
}

impl fmt::Debug for Element {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &*self {
            Element::Rulename(s) => write!(f, "Element::Rulename({:?}.to_string())", s),
            Element::IString(s) => write!(f, "Element::IString({:?}.to_string())", s),
            Element::SString(s) => write!(f, "Element::SString({:?}.to_string())", s),
            Element::NumberValue(n) => write!(f, "Element::NumberValue({:?})", n),
            Element::ValueRange(t) => write!(f, "Element::ValueRange({:?})", t),
            Element::ValueSequence(v) => write!(f, "Element::ValueSequence(vec!{:?})", v),
            Element::ProseValue(s) => write!(f, "Element::ProseValue({:?}.to_string())", s),
            Element::Sequence(v) => write!(f, "Element::Sequence(vec!{:?})", v),
            Element::Selection(v) => write!(f, "Element::Selection(vec!{:?})", v),
        }
    }
}

/// Repeat.
#[derive(PartialEq, Debug, Clone)]
pub struct Repeat {
    pub min: Option<usize>,
    pub max: Option<usize>,
}

impl Repeat {
    pub fn new(min: Option<usize>, max: Option<usize>) -> Repeat {
        Repeat { min, max }
    }
}

/// Element with repeat.
#[derive(PartialEq, Debug, Clone)]
pub struct Repetition {
    pub repeat: Option<Repeat>,
    pub element: Element,
}

impl Repetition {
    pub fn new(repeat: Option<Repeat>, element: Element) -> Repetition {
        Repetition { repeat, element }
    }
}

/// Rulelist.
pub type Rulelist = HashMap<String, Repetition>;