easy_regex/
list.rs

1//! Creates list patterns.
2//! 
3//! Two methods are used to create a list. The main one is the [`list`](../struct.EasyRegex.html#method.list) method and the other is
4//! the [`into_list`](../struct.EasyRegex.html#method.into_list).
5//! They both use the [`literal`](../struct.EasyRegex.html#method.literal) method internally and in some ways are similar 
6//! to the [`group`](../struct.EasyRegex.html#method.group) and [`into_group`](../struct.EasyRegex.html#method.into_group) methods.
7
8use crate::{settings::Settings, EasyRegex};
9
10impl EasyRegex {
11    /// Creates a list of expressions.
12    /// 
13    /// This method takes an expression (a segment of entire pattern) followed
14    /// by a set of settings (```Settings``` struct) that will be concatenated/inserted to the expression itself,
15    /// outputing the previous pattern followed by this list.
16    ///
17    /// # Examples
18    ///
19    /// ```
20    /// use easy_regex::{EasyRegex, settings::base::DEFAULT};
21
22    /// let result = EasyRegex::new_section().list("some_list", &DEFAULT);
23    /// assert_eq!("[some_list]", result.get_regex().unwrap().as_str());
24    /// ```
25    pub fn list(self, expression: &str, settings: &Settings) -> EasyRegex {
26        let mut final_result = expression.to_string();
27        final_result = format!("[{}]", final_result);
28
29        let final_result = self.literal(&final_result, &settings);
30        final_result
31    }
32
33    /// Turns the previous expressions into a list.
34    ///
35    /// # Examples
36    ///
37    /// ```
38    /// use easy_regex::{EasyRegex, settings::base::OPTIONAL_UNGREEDY};
39    ///
40    /// let result = EasyRegex::new(r"a-z").into_list(&OPTIONAL_UNGREEDY);
41    /// assert_eq!(r"[a-z]??", result.get_regex().unwrap().as_str());
42    /// ```
43    pub fn into_list(self, settings: &Settings) -> EasyRegex {
44        let raw_result = format!("[{}]", self.0);
45        let final_result = EasyRegex::new_section().literal(&raw_result, &settings);
46        final_result
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use self::EasyRegex;
53    use super::*;
54    use crate::settings::{base::*, Flags};
55
56    #[test]
57    fn list_works() {
58        let initial_exp = EasyRegex::new("initial");
59        let result = initial_exp.list("abcd", &DEFAULT);
60        assert_eq!(result.0, "initial[abcd]");
61    }
62
63    #[test]
64    fn another_list_works() {
65        let initial_exp = EasyRegex::new("initial");
66        let result = initial_exp.list(
67            "abcd",
68            &Settings {
69                is_nil_or_more: true,
70                is_one_or_more: true,
71                ..Default::default()
72            },
73        );
74        assert_eq!("initial[abcd]*+", result.get_regex().unwrap().as_str());
75    }
76
77    #[test]
78    fn list_with_flag_and_settings_works() {
79        let result = EasyRegex::new_section().list(
80            "list",
81            &Settings {
82                range: Some((Some(2), None)),
83                flags: Some(Flags::Insensitive),
84                ..Default::default()
85            },
86        );
87
88        assert_eq!("(?i)[list]{2,}", result.0);
89    }
90}