44#![ warn( clippy:: missing_docs_in_private_items) ]
55
66//! # Regex for Humans
7+ //! ## About
78//! The goal of this crate is simple: give everybody the power of regular expressions without having
89//! to learn the complicated syntax. It is inspired by [ReadableRegex.jl](https://github.com/jkrumbiegel/ReadableRegex.jl).
910
11+ //!## Example usage
12+ //!### Matching a date
13+ //!If you want to match a date of the format `2021-10-30`, you would use the following code to generate a regex:
14+ //!```rust
15+ //!let hr = human_regex::HumanRegex::new()
16+ //! .begin()
17+ //! .exactly(4, human_regex::DIGIT)
18+ //! .text("-")
19+ //! .exactly(2, human_regex::DIGIT)
20+ //! .text("-")
21+ //! .exactly(2, human_regex::DIGIT)
22+ //! .end();
23+ //!assert!(hr.is_match("2014-01-01"));
24+ //!```
25+ //!Specifically, this chunk of code would yield the regex `^\d{4}-\d{2}-\d{2}$`, which is exactly what we want!
26+
1027use regex:: Regex ;
1128
12- /// A unit struct for the digit character class (i.e., the digits 0 through 9)
13- struct Digit ;
14- /// A unit struct for the non-digit character class (i.e., everything BUT the digits 0-9)
15- struct NonDigit ;
16- /// A unit struct for the word character class (i.e., all alphanumeric characters plus underscore)
17- struct Word ;
18- /// A unit struct for the non-word character class (i.e., everything BUT the alphanumeric characters plus underscore)
19- struct NonWord ;
20- /// A unit structure for the whitespace character class (i.e., space and tab)
21- struct Whitespace ;
22- /// A unit structure for the whitespace character class (i.e., everything BUT space and tab)
23- struct NonWhitespace ;
29+ /// A constant for the digit character class (i.e., the digits 0 through 9)
30+ pub const DIGIT : & str = r"\d" ;
31+ /// A constant for the non-digit character class (i.e., everything BUT the digits 0-9)
32+ pub const NON_DIGIT : & str = r"\D" ;
33+ /// A constant for the word character class (i.e., all alphanumeric characters plus underscore)
34+ pub const WORD : & str = r"\w" ;
35+ /// A constant for the non-word character class (i.e., everything BUT the alphanumeric characters plus underscore)
36+ pub const NON_WORD : & str = r"\W" ;
37+ /// A constant for the whitespace character class (i.e., space and tab)
38+ pub const WHITESPACE : & str = r"\t" ;
39+ /// A constant for the whitespace character class (i.e., everything BUT space and tab)
40+ pub const NON_WHITESPACE : & str = r"\T" ;
2441
2542/// The HumanRegex struct which maintains and updates the regex string
2643#[ derive( Default ) ]
@@ -30,13 +47,57 @@ pub struct HumanRegex {
3047}
3148
3249impl HumanRegex {
50+ /// Generate a new HumanRegex with a blank regex_string
51+ pub fn new ( ) -> Self {
52+ HumanRegex {
53+ regex_string : String :: from ( "" ) ,
54+ }
55+ }
56+
57+ /// Match exactly a certain number of a certain target
58+ pub fn exactly ( & self , n : u8 , target : & str ) -> Self {
59+ let new_regex = format ! ( "{}{}{{{}}}" , self . regex_string, target, n) ;
60+ HumanRegex {
61+ regex_string : new_regex,
62+ }
63+ }
64+
65+ /// Add text directly to the match string
66+ pub fn text ( & self , text : & str ) -> Self {
67+ let new_regex = format ! ( "{}{}" , self . regex_string, text) ;
68+ HumanRegex {
69+ regex_string : new_regex,
70+ }
71+ }
72+
73+ /// Represents the beginning of the text
74+ pub fn begin ( & self ) -> Self {
75+ let new_regex = format ! ( "{}{}" , self . regex_string, r"^" ) ;
76+ HumanRegex {
77+ regex_string : new_regex,
78+ }
79+ }
80+
81+ /// Represents the end of the text
82+ pub fn end ( & self ) -> Self {
83+ let new_regex = format ! ( "{}{}" , self . regex_string, r"$" ) ;
84+ HumanRegex {
85+ regex_string : new_regex,
86+ }
87+ }
88+
3389 /// Generates a new human regex directly from a regex string
34- pub fn new ( regex_string : & str ) -> Self {
90+ pub fn from_regex_string ( regex_string : & str ) -> Self {
3591 HumanRegex {
3692 regex_string : String :: from ( regex_string) ,
3793 }
3894 }
3995
96+ /// Returns the current state of the constructed regex string
97+ pub fn get_regex_string ( & self ) -> & String {
98+ return & self . regex_string ;
99+ }
100+
40101 /// Checks whether or not a string matches with the constructed regex
41102 pub fn is_match ( & self , string_to_match : & str ) -> bool {
42103 let re = Regex :: new ( & * self . regex_string ) . unwrap ( ) ;
0 commit comments