easy_regex/
lib.rs

1//! This crate implements another way of writing regualar expressions as if they are pseudocodes,
2//! they will be easier to understand and debug, especially the long ones.
3//! 
4//! To start writing a regex using this crate, there are several ways (see [`head_or_tail`](head_or_tail)) of which 
5//! [`new`](struct.EasyRegex.html#method.new) and [`new_section`](struct.EasyRegex.html#method.new_section) are the common methods.
6//! The [`new`](struct.EasyRegex.html#method.new) method takes an ```&str``` as input and makes a raw expression while
7//! the [`new_section`](struct.EasyRegex.html#method.new_section)
8//! method needs no input and basically creates an empty string to write the intented expressions by method chaining.
9//! To take the prepared regex out of the chain, the last method will be [`get_regex`](struct.EasyRegex.html#method.get_regex) 
10//! which outputs a ```Result``` including a regex of type ```Regex``` or an ```Error```.
11//! The [`get_regex`](struct.EasyRegex.html#method.get_regex) will in fact use 
12//! the [`RegexBuilder::new`](https://docs.rs/regex/latest/regex/struct.RegexSetBuilder.html#method.new)
13//! and [`RegexBuilder::build`](https://docs.rs/regex/latest/regex/struct.RegexSetBuilder.html#method.build) methods of
14//! the [regex](https://crates.io/crates/regex) crate.
15
16use regex::{Regex, Error};
17
18pub mod literal;
19pub mod group;
20pub mod list;
21pub mod settings;
22pub mod collection;
23pub mod head_or_tail;
24pub mod metacharacters;
25pub mod helpers;
26
27#[macro_use]
28extern crate lazy_static;
29
30#[derive(Debug, Clone)]
31/// Main struct includes methods to be chained together in order to create a regular expression.
32pub struct EasyRegex(String);
33
34impl EasyRegex {
35    /// Creates an ```EasyRegex``` instance, having initial raw pattern.
36    pub fn new(raw: &str) -> Self {
37        EasyRegex(raw.to_string())
38    }
39
40    /// Creates an empty ```EasyRegex``` instance, useful for start of a pattern.
41    pub fn new_section() -> Self {
42        EasyRegex(String::new())
43    }
44
45    /// Retrieves the prepared regular expression as a ```Result```.
46    pub fn get_regex(self) -> Result<Regex, Error> {
47        let regex = regex::RegexBuilder::new(&self.0);
48        regex.build()
49    }
50}
51
52#[doc = include_str!("../README.md")]
53#[cfg(doctest)]
54pub struct ReadmeDoctests;