easy_regex/settings/
mod.rs

1//! Flags, Special Characters etc. classified as Settings to be used as necessary meta information for patterns.
2//! 
3//! Here are the meta data such as flags, boundaries needed to be added to regular expressions.
4//! For most methods except [`group`](../struct.EasyRegex.html#method.group) and [`named_group`](../struct.EasyRegex.html#method.named_group),
5//! the ```Settings``` struct would be used and for these two, the ```GroupSettings``` struct which 
6//! has one extra meta that is ```is_non_capture```.
7//! There is a default implemention for both structs for ease of use and to reduce typing repetitive meta over and over as well as shortening code length.
8//! Two methods are provided for setting an exact or a range of repetitions for an expression in case the only meta is either of them.
9//! Those are ```exactly``` and ```range``` for non-group expressions called ```base``` settings and ```grp_exactly``` and ```grp_range``` for ```group``` settings.
10
11pub mod base;
12pub mod group;
13
14#[derive(Clone, Copy)]
15/// Regular Expression Flags
16pub enum Flags {
17    Insensitive,
18    Multiline,
19    DotMatchNewLine,
20    IgnoreWhitespace,
21    Sensitive,
22    SingleLine,
23    DotDisMatchNewLine,
24    IncludeWhitespace,
25}
26
27impl Flags {
28    /// Converts an enum flag into its equivalent meaning.
29    /// 
30    ///# Examples
31    /// ```
32    /// use easy_regex::settings::Flags;
33    /// 
34    /// println!("{}", Flags::Insensitive.as_str()); // outputs ?i
35    /// ```
36    pub fn as_str(&self) -> &'static str {
37        match self {
38            Flags::Insensitive => "?i",
39            Flags::Multiline => "?m",
40            Flags::DotMatchNewLine => "?s",
41            Flags::IgnoreWhitespace => "?x",
42            Flags::Sensitive => "?-i",
43            Flags::SingleLine => "?-m",
44            Flags::DotDisMatchNewLine => "?-s",
45            Flags::IncludeWhitespace => "?-x",
46        }
47    }
48}
49
50/// A set of meta indicating what special characters (quantifiers), boundaries and flags should be added to an expression.
51pub struct Settings {
52    pub is_optional: bool,
53    pub is_optional_ungreedy: bool,
54    pub is_one_or_more: bool,
55    pub is_nil_or_more: bool,
56    pub with_left_boundary: bool,
57    pub with_left_non_boundary: bool,
58    pub with_right_boundary: bool,
59    pub with_right_non_boundary: bool,
60    pub range: Option<(Option<u8>, Option<u8>)>,
61    pub exactly: Option<u8>,
62    pub flags: Option<Flags>,
63}
64
65impl Default for Settings {
66    fn default() -> Self {
67        Settings {
68            is_optional: false,
69            is_optional_ungreedy: false,
70            is_one_or_more: false,
71            is_nil_or_more: false,
72            with_left_boundary: false,
73            with_left_non_boundary: false,
74            with_right_boundary: false,
75            with_right_non_boundary: false,
76            range: None,
77            exactly: None,
78            flags: None,
79        }
80    }
81}
82
83impl Settings {
84    pub fn exactly(number: u8) -> Self {
85        Settings {
86            exactly: Some(number),
87            ..Default::default()
88        }
89    }
90
91    pub fn range(from: Option<u8>, to: Option<u8>) -> Self {
92        Settings {
93            range: Some((from, to)),
94            ..Default::default()
95        }
96    }
97}
98
99/// A set of meta extending ```Settings``` struct by adding a property to determine if a group is capturing or not.
100pub struct GroupSettings {
101    pub other: Settings,
102    pub is_non_capture: bool,
103}
104
105impl Default for GroupSettings {
106    fn default() -> Self {
107        GroupSettings {
108            other: Settings::default(),
109            is_non_capture: false,
110        }
111    }
112}
113
114impl GroupSettings {
115    pub fn grp_exactly(number: u8) -> Self {
116        GroupSettings {
117            other: Settings {
118                exactly: Some(number),
119                ..Default::default()
120            },
121            ..Default::default()
122        }
123    }
124
125    pub fn grp_range(from: Option<u8>, to: Option<u8>) -> Self {
126        GroupSettings {
127            other: Settings {
128                range: Some((from, to)),
129                ..Default::default()
130            },
131            ..Default::default()
132        }
133    }
134}