abnf_parser/
element.rs

1use std::collections::HashMap;
2use std::fmt;
3
4/// An individual element in an ABNF rule.
5#[derive(PartialEq, Clone)]
6pub enum Element {
7    /// rulename.
8    Rulename(String),
9    /// case insensitive string.
10    IString(String),
11    /// case seisitve string.
12    SString(String),
13    /// num-val.
14    NumberValue(u32),
15    /// range of num-val.
16    ValueRange((u32, u32)),
17    /// sequence of num-val.
18    ValueSequence(Vec<u32>),
19    /// prose-val.
20    ProseValue(String),
21    /// concatination.
22    Sequence(Vec<Repetition>),
23    /// alternation.
24    Selection(Vec<Repetition>),
25}
26
27impl fmt::Debug for Element {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match &*self {
30            Element::Rulename(s) => write!(f, "Element::Rulename({:?}.to_string())", s),
31            Element::IString(s) => write!(f, "Element::IString({:?}.to_string())", s),
32            Element::SString(s) => write!(f, "Element::SString({:?}.to_string())", s),
33            Element::NumberValue(n) => write!(f, "Element::NumberValue({:?})", n),
34            Element::ValueRange(t) => write!(f, "Element::ValueRange({:?})", t),
35            Element::ValueSequence(v) => write!(f, "Element::ValueSequence(vec!{:?})", v),
36            Element::ProseValue(s) => write!(f, "Element::ProseValue({:?}.to_string())", s),
37            Element::Sequence(v) => write!(f, "Element::Sequence(vec!{:?})", v),
38            Element::Selection(v) => write!(f, "Element::Selection(vec!{:?})", v),
39        }
40    }
41}
42
43/// Repeat.
44#[derive(PartialEq, Debug, Clone)]
45pub struct Repeat {
46    pub min: Option<usize>,
47    pub max: Option<usize>,
48}
49
50impl Repeat {
51    pub fn new(min: Option<usize>, max: Option<usize>) -> Repeat {
52        Repeat { min, max }
53    }
54}
55
56/// Element with repeat.
57#[derive(PartialEq, Debug, Clone)]
58pub struct Repetition {
59    pub repeat: Option<Repeat>,
60    pub element: Element,
61}
62
63impl Repetition {
64    pub fn new(repeat: Option<Repeat>, element: Element) -> Repetition {
65        Repetition { repeat, element }
66    }
67}
68
69/// Rulelist.
70pub type Rulelist = HashMap<String, Repetition>;