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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! The Builder of the [`Matcher`](struct.Matcher.html)
use anyhow::Result;

use crate::{
    internal::{type_of, FnStr, FnStrWithKey},
    try_into_with::TryIntoWith,
    Matcher, PathRegex, PathRegexOptions,
};

/// The Configuration of the [`Matcher`](struct.Matcher.html)
#[derive(Clone)]
pub struct MatcherOptions {
    /// Set the default delimiter for repeat parameters. (default: `'/#?'`)
    pub delimiter: String,
    /// List of characters to automatically consider prefixes when parsing.
    pub prefixes: String,
    /// When `true` the regexp will be case sensitive. (default: `false`)
    pub sensitive: bool,
    /// When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
    pub strict: bool,
    /// When `true` the regexp will match to the end of the string. (default: `true`)
    pub end: bool,
    /// When `true` the regexp will match from the beginning of the string. (default: `true`)
    pub start: bool,
    /// List of characters that can also be "end" characters.
    pub ends_with: String,
    /// Encode path tokens for use in the `Regex`.
    pub encode: FnStr,
    /// Function for decoding strings for params.
    pub decode: FnStrWithKey,
}

impl Default for MatcherOptions {
    fn default() -> Self {
        let PathRegexOptions {
            delimiter,
            prefixes,
            sensitive,
            strict,
            end,
            start,
            ends_with,
            encode,
        } = PathRegexOptions::default();
        Self {
            delimiter,
            prefixes,
            sensitive,
            strict,
            end,
            start,
            ends_with,
            encode,
            decode: |x, _| x.to_owned(),
        }
    }
}

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

impl std::fmt::Debug for MatcherOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MatcherOptions")
            .field("delimiter", &self.delimiter)
            .field("prefixes", &self.prefixes)
            .field("sensitive", &self.sensitive)
            .field("strict", &self.strict)
            .field("end", &self.end)
            .field("start", &self.start)
            .field("ends_with", &self.ends_with)
            .field("encode", &type_of(self.encode))
            .field("decode", &type_of(self.decode))
            .finish()
    }
}

/// The Builder of the [`Matcher`](struct.Matcher.html)
pub struct MatcherBuilder<I> {
    source: I,
    options: MatcherOptions,
}

impl<I> MatcherBuilder<I>
where
    I: TryIntoWith<PathRegex, PathRegexOptions>,
{
    /// Create a builder of the [`Matcher`](struct.Matcher.html)
    pub fn new(source: I) -> Self {
        Self {
            source,
            options: Default::default(),
        }
    }

    /// Create a builder of the [`Matcher`](struct.Matcher.html) with the options
    pub fn new_with_options(source: I, options: MatcherOptions) -> Self {
        Self { source, options }
    }

    /// build a builder of the [`Matcher`](struct.Matcher.html)
    pub fn build(&self) -> Result<Matcher> {
        let re = self
            .source
            .clone()
            .try_into_with(&PathRegexOptions::from(self.options.clone()))?;

        Ok(Matcher {
            re: re.clone(),
            keys: re.keys,
            options: self.options.clone(),
        })
    }
}

impl<I> MatcherBuilder<I> {
    /// List of characters to automatically consider prefixes when parsing.
    pub fn set_prefixes(&mut self, prefixes: impl AsRef<str>) -> &mut Self {
        self.options.prefixes = prefixes.as_ref().to_owned();
        self
    }

    /// When `true` the regexp will be case sensitive. (default: `false`)
    pub fn set_sensitive(&mut self, yes: bool) -> &mut Self {
        self.options.sensitive = yes;
        self
    }

    /// When `true` the regexp won't allow an optional trailing delimiter to match. (default: `false`)
    pub fn set_strict(&mut self, yes: bool) -> &mut Self {
        self.options.strict = yes;
        self
    }

    /// When `true` the regexp will match to the end of the string. (default: `true`)
    pub fn set_end(&mut self, yes: bool) -> &mut Self {
        self.options.end = yes;
        self
    }

    /// When `true` the regexp will match from the beginning of the string. (default: `true`)
    pub fn set_start(&mut self, yes: bool) -> &mut Self {
        self.options.start = yes;
        self
    }

    /// Set the default delimiter for repeat parameters. (default: `'/'`)
    pub fn set_delimiter(&mut self, de: impl AsRef<str>) -> &mut Self {
        self.options.delimiter = de.as_ref().to_owned();
        self
    }

    /// List of characters that can also be "end" characters.
    pub fn set_ends_with(&mut self, end: impl AsRef<str>) -> &mut Self {
        self.options.ends_with = end.as_ref().to_owned();
        self
    }

    /// Function for encoding input strings for output.
    pub fn set_encode(&mut self, encode: FnStr) -> &mut Self {
        self.options.encode = encode;
        self
    }

    /// Function for decoding strings for params.
    pub fn set_decode(&mut self, decode: FnStrWithKey) -> &mut Self {
        self.options.decode = decode;
        self
    }
}