path2regex/parser/
builder.rs

1//! The Builder of the [`Parser`](struct.Parser.html)
2
3#[cfg(feature = "compile")]
4use crate::CompilerOptions;
5use crate::{Parser, PathRegexOptions, DEFAULT_DELIMITER};
6
7/// The Configuration of the [`Parser`](struct.Parser.html)
8#[derive(Clone)]
9pub struct ParserOptions {
10    /// Set the default delimiter for repeat parameters. (default: `'/'`)
11    pub delimiter: String,
12    /// List of characters to automatically consider prefixes when parsing.
13    pub prefixes: String,
14}
15
16impl Default for ParserOptions {
17    fn default() -> Self {
18        Self {
19            delimiter: DEFAULT_DELIMITER.to_owned(),
20            prefixes: "./".to_owned(),
21        }
22    }
23}
24
25impl std::fmt::Debug for ParserOptions {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        f.debug_struct("ParserOptions")
28            .field("delimiter", &self.delimiter)
29            .field("prefixes", &self.prefixes)
30            .finish()
31    }
32}
33
34impl std::fmt::Display for ParserOptions {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        std::fmt::Debug::fmt(&self, f)
37    }
38}
39
40impl From<PathRegexOptions> for ParserOptions {
41    #[inline]
42    fn from(options: PathRegexOptions) -> Self {
43        let PathRegexOptions {
44            delimiter,
45            prefixes,
46            ..
47        } = options;
48        Self {
49            delimiter,
50            prefixes,
51        }
52    }
53}
54
55#[cfg(feature = "compile")]
56impl From<CompilerOptions> for ParserOptions {
57    #[inline]
58    fn from(options: CompilerOptions) -> Self {
59        let CompilerOptions {
60            delimiter,
61            prefixes,
62            ..
63        } = options;
64        Self {
65            delimiter,
66            prefixes,
67        }
68    }
69}
70
71/// The Builder of the [`Parser`](struct.Parser.html)
72#[derive(Debug, Clone)]
73pub struct ParserBuilder(ParserOptions);
74
75impl ParserBuilder {
76    /// Create a [`Parser`](struct.Parser.html) Builder
77    pub fn new() -> Self {
78        Self(Default::default())
79    }
80
81    /// Finish to build a [`Parser`](struct.Parser.html)
82    pub fn build(&self) -> Parser {
83        Parser(self.0.clone())
84    }
85
86    /// Set the default delimiter for repeat parameters. (default: `'/'`)
87    pub fn set_delimiter<S>(&mut self, delimiter: S) -> &mut Self
88    where
89        S: AsRef<str>,
90    {
91        self.0.delimiter = delimiter.as_ref().to_owned();
92        self
93    }
94
95    /// List of characters to automatically consider prefixes when parsing.
96    pub fn set_prefixes<S>(&mut self, prefixes: S) -> &mut Self
97    where
98        S: AsRef<str>,
99    {
100        self.0.prefixes = prefixes.as_ref().to_owned();
101        self
102    }
103}
104
105impl Default for ParserBuilder {
106    #[inline]
107    fn default() -> Self {
108        Self::new()
109    }
110}