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
//! The Builder of the [`Parser`](struct.Parser.html)

#[cfg(feature = "compile")]
use crate::CompilerOptions;
use crate::{Parser, PathRegexOptions, DEFAULT_DELIMITER};

/// The Configuration of the [`Parser`](struct.Parser.html)
#[derive(Clone)]
pub struct ParserOptions {
    /// Set the default delimiter for repeat parameters. (default: `'/'`)
    pub delimiter: String,
    /// List of characters to automatically consider prefixes when parsing.
    pub prefixes: String,
}

impl Default for ParserOptions {
    fn default() -> Self {
        Self {
            delimiter: DEFAULT_DELIMITER.to_owned(),
            prefixes: "./".to_owned(),
        }
    }
}

impl std::fmt::Debug for ParserOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ParserOptions")
            .field("delimiter", &self.delimiter)
            .field("prefixes", &self.prefixes)
            .finish()
    }
}

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

impl From<PathRegexOptions> for ParserOptions {
    #[inline]
    fn from(options: PathRegexOptions) -> Self {
        let PathRegexOptions {
            delimiter,
            prefixes,
            ..
        } = options;
        Self {
            delimiter,
            prefixes,
        }
    }
}

#[cfg(feature = "compile")]
impl From<CompilerOptions> for ParserOptions {
    #[inline]
    fn from(options: CompilerOptions) -> Self {
        let CompilerOptions {
            delimiter,
            prefixes,
            ..
        } = options;
        Self {
            delimiter,
            prefixes,
        }
    }
}

/// The Builder of the [`Parser`](struct.Parser.html)
#[derive(Debug, Clone)]
pub struct ParserBuilder(ParserOptions);

impl ParserBuilder {
    /// Create a [`Parser`](struct.Parser.html) Builder
    pub fn new() -> Self {
        Self(Default::default())
    }

    /// Finish to build a [`Parser`](struct.Parser.html)
    pub fn build(&self) -> Parser {
        Parser(self.0.clone())
    }

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

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

impl Default for ParserBuilder {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}