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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::{AsRegex, Condition, Result};
use regex::Regex;

/// Represents a regex type. This enum is used to create the smallest regex statement.
/// For example, `Type::Digit` will create the regex `\d`.
///
/// # Examples
/// ```
/// use magic_regexp::{OneOrMore, Type::Digit};
///
/// let input = OneOrMore(Digit);
/// assert_eq!(input.to_string(), r"(\d+)"); // Note that the regex is wrapped in parentheses.
/// ```
pub enum Type {
    Digit,
    NotDigit,
    WordBoundary,
    NotWordBoundary,
    Word,
    WordChar,
    NotWordChar,
    Text(String),
    Options(String),
    Char,
    Whitespace,
    NotWhitespace,
    Letter,
    NotLetter,
    LetterLowercase,
    NotLetterLowercase,
    LetterUppercase,
    NotLetterUppercase,
    Tab,
    NotTab,
    Linefeed,
    NotLinefeed,
    CarriageReturn,
    NotCarriageReturn,
}

impl AsRegex for Type {}
impl ToString for Type {
    fn to_string(&self) -> String {
        let txt;
        match self {
            Type::Digit => r"\d",
            Type::NotDigit => r"\D",
            Type::WordBoundary => r"\b",
            Type::NotWordBoundary => r"\B",
            Type::Word => r"\b\w+\b",
            Type::WordChar => r"\w",
            Type::NotWordChar => r"\W",
            Type::Char => r".",
            Type::Text(text) => text,
            Type::Whitespace => r"\s",
            Type::NotWhitespace => r"\S",
            Type::Letter => r"[a-zA-Z]",
            Type::NotLetter => r"[^a-zA-Z]",
            Type::LetterLowercase => r"[a-z]",
            Type::NotLetterLowercase => r"[^a-z]",
            Type::LetterUppercase => r"[A-Z]",
            Type::NotLetterUppercase => r"[^A-Z]",
            Type::Tab => r"\t",
            Type::NotTab => r"^\t",
            Type::Linefeed => r"\n",
            Type::NotLinefeed => r"^\n",
            Type::CarriageReturn => r"\r",
            Type::NotCarriageReturn => r"^\r",
            Type::Options(options) => {
                txt = format!("[{}]", options);
                txt.as_str()
            }
        }
        .to_string()
    }
}

/// Returns the opposite of the given type.
/// For example, `Type::Digit` will return `Type::NotDigit`.
/// Returns the same type if it is not a type that can be negated.
///
/// Panics, if the given type is `Type::Options` and the given string is empty.
///
/// # Examples
/// ```
/// use magic_regexp::{OneOrMore, not, Options};
///
/// let input = OneOrMore(not(not(Options("01".to_string()))));
/// assert_eq!(input.to_string(), r"([01]+)");
/// ```
pub fn not(t: Type) -> Type {
    match t {
        Type::Digit => Type::NotDigit,
        Type::NotDigit => Type::Digit,
        Type::WordBoundary => Type::NotWordBoundary,
        Type::NotWordBoundary => Type::WordBoundary,
        Type::WordChar => Type::NotWordChar,
        Type::NotWordChar => Type::WordChar,
        Type::Whitespace => Type::NotWhitespace,
        Type::NotWhitespace => Type::Whitespace,
        Type::Letter => Type::NotLetter,
        Type::NotLetter => Type::Letter,
        Type::LetterLowercase => Type::NotLetterLowercase,
        Type::NotLetterLowercase => Type::LetterLowercase,
        Type::LetterUppercase => Type::NotLetterUppercase,
        Type::NotLetterUppercase => Type::LetterUppercase,
        Type::Tab => Type::NotTab,
        Type::NotTab => Type::Tab,
        Type::Linefeed => Type::NotLinefeed,
        Type::NotLinefeed => Type::Linefeed,
        Type::CarriageReturn => Type::NotCarriageReturn,
        Type::NotCarriageReturn => Type::CarriageReturn,
        Type::Text(t) => Type::Text(format!("^{}", t)),
        Type::Options(t) => {
            if let Some(first) = t.chars().next() {
                let opt = if first == '^' {
                    t[1..].to_string()
                } else {
                    format!("^{}", t)
                };
                Type::Options(opt)
            } else {
                panic!("Invalid options: {}", t);
            }
        }
        _ => t,
    }
}

/// This is a regex input that can be used to match a single character or a group of characters.
/// Can be used to create a regex that matches a single character or a group of characters.
/// For example, `Input::Exactly(Type::Digit)` will match a single digit.
///
/// # Example
/// ```
/// use magic_regexp::{create_reg_exp, Input, Type};
///
/// let regex = create_reg_exp(Input::Exactly(Type::Digit)).unwrap();
/// assert!(regex.is_match("1"));
/// assert!(!regex.is_match("12"));
/// assert!(regex.is_match("1 2"));
/// ```
///
/// # Example
/// ```
/// use magic_regexp::{create_reg_exp, Input, Type};
///
/// let regex = create_reg_exp(Input::OneOrMore(Type::Digit)).unwrap();
/// assert!(regex.is_match("1"));
/// assert!(regex.is_match("12"));
/// assert!(regex.is_match("1 2"));
/// ```
///
/// # Example
/// ```
/// use magic_regexp::{create_reg_exp, Input, Type};
///
/// let regex = create_reg_exp(Input::Maybe(Type::Digit)).unwrap();
/// assert!(regex.is_match("1"));
/// assert!(regex.is_match(""));
/// assert!(regex.is_match("12"));
/// assert!(regex.is_match("a"));
/// assert!(regex.is_match("1 2"));
/// ```
pub enum Input {
    OneOrMore(Type),
    Exactly(Type),
    Maybe(Type),
    Times(Type, usize),
}

impl ToString for Input {
    /// Returns a string representation of the input.
    /// For example, `Input::Exactly(Type::Digit)` will return `\d`.
    ///
    /// # Example
    /// ```
    /// use magic_regexp::{Exactly, Digit, AsRegex, create_reg_exp};
    ///
    /// let regex = create_reg_exp(Exactly(Digit)).unwrap();
    /// assert!(regex.is_match("1"));
    /// assert!(!regex.is_match("12"));
    /// ```
    ///
    /// # Example
    /// ```
    /// use magic_regexp::{Input, Type};
    ///
    /// let input = Input::Exactly(Type::Text("abc".into()));
    /// assert_eq!(input.to_string(), "abc");
    /// let input = Input::Exactly(Type::Text(".".to_string()));
    /// assert_eq!(input.to_string(), r"\.");
    /// ```
    ///
    /// # Example
    /// ```
    /// use magic_regexp::{not, OneOrMore, Options};
    /// use regex::Regex;
    ///
    /// let input = OneOrMore(not(Options("01".to_string())));
    /// assert_eq!(input.to_string(), r"([^01]+)");
    /// let re = Regex::new(&input.to_string()).unwrap();
    /// assert_eq!(re.replace("1078910", ""), "1010");
    /// ```
    fn to_string(&self) -> String {
        const ESCAPE_REPLACE_RE: &str = r"[.*+?^${}()|[\\]\\/]";

        match self {
            Input::OneOrMore(t) => format!("({}+)", t.to_string()),
            Input::Exactly(t) => match t {
                Type::Text(t) => Regex::new(ESCAPE_REPLACE_RE)
                    .expect("Invalid replace_all regex")
                    .replace_all(t, r"\$0")
                    .to_string(),
                _ => format!(r"\b{}\b", t.to_string()),
            },
            Input::Maybe(t) => format!("({}?)", t.to_string()),
            Input::Times(t, n) => format!("{}{{{}}}", t.to_string(), n),
        }
    }
}

impl AsRegex for Input {
    fn as_regex(&self) -> Result<Regex> {
        Ok(Regex::new(&self.to_string())?)
    }
}

/// Returns a Regex, which chains the 2 given regexes with an `and` operator.
///
/// # Example
/// ```
/// use magic_regexp::{create_reg_exp, Condition, Exactly, Digit, LetterLowercase};
///
/// let regex = create_reg_exp(Exactly(Digit).or(Exactly(LetterLowercase))).unwrap();
/// assert!(regex.is_match("1"));
/// assert!(regex.is_match("a"));
/// assert!(!regex.is_match("A"));
/// assert!(!regex.is_match("12"));
/// assert!(!regex.is_match("1a"));
/// assert!(regex.is_match("1 a"));
/// ```
impl Condition for Input {}

impl Input {
    /// This defines the entire input so far as a named capture group.
    ///
    /// # Example
    /// ```
    /// use magic_regexp::{create_reg_exp, Condition, Exactly, Digit, LetterLowercase, OneOrMore};
    ///
    /// let regex = create_reg_exp(OneOrMore(Digit).grouped_as("digits")).unwrap();
    /// assert_eq!(&regex.captures("1").unwrap()["digits"], "1");
    /// ```
    pub fn grouped_as(&self, name: &str) -> Regex {
        Regex::new(&format!(r"(?P<{}>{})", name, self.to_string())).expect("Invalid regex")
    }

    /// This defines the entire input so far as a named capture group.
    /// This is an alias for `grouped_as`.
    pub fn r#as(&self, name: &str) -> Regex {
        self.grouped_as(name)
    }

    /// This defines the entire input so far as an anonymous group.
    ///
    /// # Example
    /// ```
    /// use magic_regexp::{create_reg_exp, Condition, Exactly, Digit, LetterLowercase, OneOrMore, Type, Char, Whitespace, Maybe, Options};
    /// use regex::Regex;
    ///
    /// let regex = create_reg_exp(OneOrMore(Digit).grouped()
    ///     .and(Exactly(Whitespace))
    ///     .and(OneOrMore(Char).or(Exactly(Whitespace)).optionally())
    ///     .and(OneOrMore(Digit).grouped())
    /// ).unwrap();
    /// assert_eq!(&regex.captures("1 5").unwrap()[1], "1");
    /// assert_eq!(&regex.captures("134 23").unwrap()[1], "134");
    /// // The folloing example is not really useful, because it shows only the first match.
    /// // The second match is not captured. See the next example for a more useful example.
    /// assert_eq!(&regex.captures("this is the 134 test 213").unwrap()[1], "134");
    /// // This is a bit more complex, because we are using anonymous groups in regex.
    /// let cap = &regex
    ///     .find_iter("multiple numbers 134 2123")
    ///     .filter_map(|digits| digits.as_str().parse().ok())
    ///     .collect::<Vec<String>>()
    ///     .join(" ");
    /// let expected = ["134", "2123"].join(" ");
    /// assert_eq!(cap, &expected);
    /// ```
    ///
    pub fn grouped(&self) -> Regex {
        Regex::new(&format!(r"({})", self.to_string())).expect("Invalid regex")
    }
}

impl AsRegex for Regex {}
impl Condition for Regex {}