Skip to content

Commit

Permalink
We have functionality! Woo!
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccomb committed Oct 30, 2021
1 parent ea72de9 commit 0401e80
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "human_regex"
version = "0.1.0"
version = "0.1.1"
authors = ["Chris McComb <[email protected]>"]
description = "A regex library for humans"
edition = "2021"
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!
87 changes: 74 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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();
Expand Down
20 changes: 19 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
#[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]
fn check_default() {
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"))
}
}

0 comments on commit 0401e80

Please sign in to comment.