diff --git a/Cargo.toml b/Cargo.toml index 40b46a4..3f44af7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "human_regex" -version = "0.1.0" +version = "0.1.1" authors = ["Chris McComb "] description = "A regex library for humans" edition = "2021" diff --git a/README.md b/README.md index a736fdc..a131e5a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,22 @@ | ⚠️ This package is under active development and doesn't do much yet. | | -------------------------------------------------------------------------- | # Regex for Humans +## About The goal of this crate is simple: give everybody the power of regular expressions without having to learn the complicated syntax. It is inspired by [ReadableRegex.jl](https://github.com/jkrumbiegel/ReadableRegex.jl). +## Example usage +### Matching a date +If you want to match a date of the format `2021-10-30`, you would use the following code to generate a regex: +```rust +let hr = human_regex::HumanRegex::new() + .begin() + .exactly(4, human_regex::DIGIT) + .text("-") + .exactly(2, human_regex::DIGIT) + .text("-") + .exactly(2, human_regex::DIGIT) + .end(); +assert!(hr.is_match("2014-01-01")); +``` +Specifically, this chunk of code would yield the regex `^\d{4}-\d{2}-\d{2}$`, which is exactly what we want! \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9142d9d..0e98545 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,23 +4,40 @@ #![warn(clippy::missing_docs_in_private_items)] //! # Regex for Humans +//! ## About //! The goal of this crate is simple: give everybody the power of regular expressions without having //! to learn the complicated syntax. It is inspired by [ReadableRegex.jl](https://github.com/jkrumbiegel/ReadableRegex.jl). +//!## Example usage +//!### Matching a date +//!If you want to match a date of the format `2021-10-30`, you would use the following code to generate a regex: +//!```rust +//!let hr = human_regex::HumanRegex::new() +//! .begin() +//! .exactly(4, human_regex::DIGIT) +//! .text("-") +//! .exactly(2, human_regex::DIGIT) +//! .text("-") +//! .exactly(2, human_regex::DIGIT) +//! .end(); +//!assert!(hr.is_match("2014-01-01")); +//!``` +//!Specifically, this chunk of code would yield the regex `^\d{4}-\d{2}-\d{2}$`, which is exactly what we want! + use regex::Regex; -/// A unit struct for the digit character class (i.e., the digits 0 through 9) -struct Digit; -/// A unit struct for the non-digit character class (i.e., everything BUT the digits 0-9) -struct NonDigit; -/// A unit struct for the word character class (i.e., all alphanumeric characters plus underscore) -struct Word; -/// A unit struct for the non-word character class (i.e., everything BUT the alphanumeric characters plus underscore) -struct NonWord; -/// A unit structure for the whitespace character class (i.e., space and tab) -struct Whitespace; -/// A unit structure for the whitespace character class (i.e., everything BUT space and tab) -struct NonWhitespace; +/// A constant for the digit character class (i.e., the digits 0 through 9) +pub const DIGIT: &str = r"\d"; +/// A constant for the non-digit character class (i.e., everything BUT the digits 0-9) +pub const NON_DIGIT: &str = r"\D"; +/// A constant for the word character class (i.e., all alphanumeric characters plus underscore) +pub const WORD: &str = r"\w"; +/// A constant for the non-word character class (i.e., everything BUT the alphanumeric characters plus underscore) +pub const NON_WORD: &str = r"\W"; +/// A constant for the whitespace character class (i.e., space and tab) +pub const WHITESPACE: &str = r"\t"; +/// A constant for the whitespace character class (i.e., everything BUT space and tab) +pub const NON_WHITESPACE: &str = r"\T"; /// The HumanRegex struct which maintains and updates the regex string #[derive(Default)] @@ -30,13 +47,57 @@ pub struct HumanRegex { } impl HumanRegex { + /// Generate a new HumanRegex with a blank regex_string + pub fn new() -> Self { + HumanRegex { + regex_string: String::from(""), + } + } + + /// Match exactly a certain number of a certain target + pub fn exactly(&self, n: u8, target: &str) -> Self { + let new_regex = format!("{}{}{{{}}}", self.regex_string, target, n); + HumanRegex { + regex_string: new_regex, + } + } + + /// Add text directly to the match string + pub fn text(&self, text: &str) -> Self { + let new_regex = format!("{}{}", self.regex_string, text); + HumanRegex { + regex_string: new_regex, + } + } + + /// Represents the beginning of the text + pub fn begin(&self) -> Self { + let new_regex = format!("{}{}", self.regex_string, r"^"); + HumanRegex { + regex_string: new_regex, + } + } + + /// Represents the end of the text + pub fn end(&self) -> Self { + let new_regex = format!("{}{}", self.regex_string, r"$"); + HumanRegex { + regex_string: new_regex, + } + } + /// Generates a new human regex directly from a regex string - pub fn new(regex_string: &str) -> Self { + pub fn from_regex_string(regex_string: &str) -> Self { HumanRegex { regex_string: String::from(regex_string), } } + /// Returns the current state of the constructed regex string + pub fn get_regex_string(&self) -> &String { + return &self.regex_string; + } + /// Checks whether or not a string matches with the constructed regex pub fn is_match(&self, string_to_match: &str) -> bool { let re = Regex::new(&*self.regex_string).unwrap(); diff --git a/tests/test.rs b/tests/test.rs index 42363d0..d2d8d01 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,8 +1,9 @@ #[cfg(test)] mod tests { + #[test] fn check_direct_regex() { - let check_match = human_regex::HumanRegex::new(r"^\d{4}-\d{2}-\d{2}$"); + let check_match = human_regex::HumanRegex::from_regex_string(r"^\d{4}-\d{2}-\d{2}$"); assert!(check_match.is_match("2014-01-01")) } #[test] @@ -10,4 +11,21 @@ mod tests { let check_match = human_regex::HumanRegex::default(); assert!(check_match.is_match("")) } + #[test] + fn check_new() { + let check_match = human_regex::HumanRegex::new(); + assert!(check_match.is_match("")) + } + #[test] + fn check_exactly_plus_text() { + let check_match = human_regex::HumanRegex::new() + .begin() + .exactly(4, human_regex::DIGIT) + .text("-") + .exactly(2, human_regex::DIGIT) + .text("-") + .exactly(2, human_regex::DIGIT) + .end(); + assert!(check_match.is_match("2014-01-01")) + } }