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
#[cfg(feature = "handparser")]
mod hand_parser;
#[cfg(feature = "with-nom")]
mod nom_parser;
use regex::Regex;
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum ParseError {
    #[error("Cannot parse expression")]
    ParseError(String),
    #[error("Embedded regex cannot compile")]
    BadRegex(String),
}

#[cfg(feature = "handparser")]
pub fn compile_impl(pattern: &str, dstart: char, dend: char) -> Result<Regex, ParseError> {
    let parsed = hand_parser::parse(pattern, dstart, dend)
        .map_err(|err| ParseError::ParseError(err.to_string()))?;
    let mut out = String::new();

    for exp in parsed {
        match exp {
            hand_parser::Token::Regex(re) => out.push_str(&re),
            hand_parser::Token::String(s) => {
                out.push_str(&regex::escape(&s));
            }
        }
    }

    out = format!("^{}$", out);
    Regex::new(&out).map_err(|err| ParseError::BadRegex(err.to_string()))
}

#[cfg(feature = "with-nom")]
fn compile_impl(pattern: &str, dstart: char, dend: char) -> Result<Regex, ParseError> {
    let (rest, parsed) = nom_parser::parse(pattern, dstart, dend)
        .map_err(|err| ParseError::ParseError(err.to_string()))?;
    if !rest.is_empty() {
        return Err(ParseError::ParseError(format!(
            "input not consumed entirely: {}",
            rest
        )));
    }
    let mut out = String::new();

    for exp in parsed {
        match exp {
            nom_parser::Token::Regex(re) => out.push_str(re),
            nom_parser::Token::String(s) => {
                out.push_str(&regex::escape(s));
            }
        }
    }
    out = format!("^{}$", out);
    Regex::new(&out).map_err(|err| ParseError::BadRegex(err.to_string()))
}

/// Compile a route pattern. Get back a `Regex` which you can use as you see fit.
///
/// # Errors
///
/// This function will return an error if parsing or regex compilation fails
pub fn compile(pattern: &str, dstart: char, dend: char) -> Result<Regex, ParseError> {
    compile_impl(pattern, dstart, dend)
}
/// Match a route pattern.
///
/// # Errors
///
/// This function will return an error if parsing or regex compilation fails
pub fn is_match(pattern: &str, dstart: char, dend: char, text: &str) -> Result<bool, ParseError> {
    let re = compile(pattern, dstart, dend)?;

    Ok(re.is_match(text))
}
#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn match_glob() {
        assert_eq!(is_match("foo/<.*>", '<', '>', "foo/bar").unwrap(), true);
    }

    #[test]
    fn match_nested() {
        assert_eq!(
            is_match("foo/{a{1,4}}/", '{', '}', "foo/aaa/").unwrap(),
            true
        );
    }

    #[test]
    fn dont_match_nested() {
        assert_eq!(
            is_match("foo/{a{1,4}}/", '{', '}', "foo/aaaaaaaa/").unwrap(),
            false
        );
    }

    #[test]
    fn match_multiple() {
        assert_eq!(
            is_match("foo/{b{1,4}}/{[0-9]+}", '{', '}', "foo/bbb/123").unwrap(),
            true
        );
    }
}