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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
macro_rules! lex_token_kind {
    ($($ty:tt $name:tt)+) => {
        #[derive(PartialEq,Eq,Copy,Clone)]
        pub(crate) enum LexTokenKind {
            $($ty,)+
        }

        impl std::fmt::Display for LexTokenKind {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                let name = match self {
                    $(LexTokenKind::$ty => $name,)+
                };
                f.write_str(name)
            }
        }

        impl std::fmt::Debug for LexTokenKind {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Display::fmt(&self, f)
            }
        }
    };
}

lex_token_kind! {
    Open "OPEN"
    Close "CLOSE"
    Pattern "PATTERN"
    Name "NAME"
    Char "CHAR"
    EscapedChar "ESCAPEDCHAR"
    Modifier "MODIFIER"
    End "END"
}

#[derive(Clone, PartialEq, Eq)]
pub(crate) struct LexToken<'a> {
    pub(crate) kind: LexTokenKind,
    pub(crate) index: usize,
    pub(crate) value: &'a str,
}

impl<'a> std::fmt::Display for LexToken<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("")
            .field("type", &self.kind)
            .field("index", &self.index)
            .field("value", &self.value)
            .finish()
    }
}

impl<'a> std::fmt::Debug for LexToken<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self, f)
    }
}

/// Parameter matches in the path
#[derive(Eq, PartialEq, Clone, Default)]
pub struct Key {
    /// The name of the parameter
    pub name: String,
    /// The prefix of the parameter
    pub prefix: String,
    /// The suffix of the parameter
    pub suffix: String,
    /// The regular in the parameter
    pub pattern: String,
    /// The modifier for the parameter
    pub modifier: String,
}

impl std::fmt::Display for Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self, f)
    }
}

impl std::fmt::Debug for Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Key")
            .field("name", &self.name)
            .field("prefix", &self.prefix)
            .field("suffix", &self.suffix)
            .field("pattern", &self.pattern)
            .field("modifier", &self.modifier)
            .finish()
    }
}

/// An abstract syntax tree node parsed by a path
#[derive(Clone, PartialEq, Eq)]
pub enum Token {
    /// A static path item
    Static(String),
    /// Parameter matches in the path
    Key(Key),
}

impl std::fmt::Display for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Token::Static(s) => f.write_str(s),
            Token::Key(Key {
                name,
                prefix,
                suffix,
                pattern,
                modifier,
            }) => f
                .debug_struct("Token")
                .field("name", name)
                .field("prefix", prefix)
                .field("suffix", suffix)
                .field("pattern", pattern)
                .field("modifier", modifier)
                .finish(),
        }
    }
}

impl std::fmt::Debug for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self, f)
    }
}