Skip to content

Commit 0401e80

Browse files
committed
We have functionality! Woo!
1 parent ea72de9 commit 0401e80

File tree

4 files changed

+110
-15
lines changed

4 files changed

+110
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "human_regex"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Chris McComb <ccmcc2012@gmail.com>"]
55
description = "A regex library for humans"
66
edition = "2021"

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
| ⚠️ This package is under active development and doesn't do much yet. |
22
| -------------------------------------------------------------------------- |
33
# Regex for Humans
4+
## About
45
The goal of this crate is simple: give everybody the power of regular expressions without having
56
to learn the complicated syntax. It is inspired by [ReadableRegex.jl](https://github.com/jkrumbiegel/ReadableRegex.jl).
67

8+
## Example usage
9+
### Matching a date
10+
If you want to match a date of the format `2021-10-30`, you would use the following code to generate a regex:
11+
```rust
12+
let hr = human_regex::HumanRegex::new()
13+
.begin()
14+
.exactly(4, human_regex::DIGIT)
15+
.text("-")
16+
.exactly(2, human_regex::DIGIT)
17+
.text("-")
18+
.exactly(2, human_regex::DIGIT)
19+
.end();
20+
assert!(hr.is_match("2014-01-01"));
21+
```
22+
Specifically, this chunk of code would yield the regex `^\d{4}-\d{2}-\d{2}$`, which is exactly what we want!

src/lib.rs

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,40 @@
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+
1027
use 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

3249
impl 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();

tests/test.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
#[cfg(test)]
22
mod tests {
3+
34
#[test]
45
fn check_direct_regex() {
5-
let check_match = human_regex::HumanRegex::new(r"^\d{4}-\d{2}-\d{2}$");
6+
let check_match = human_regex::HumanRegex::from_regex_string(r"^\d{4}-\d{2}-\d{2}$");
67
assert!(check_match.is_match("2014-01-01"))
78
}
89
#[test]
910
fn check_default() {
1011
let check_match = human_regex::HumanRegex::default();
1112
assert!(check_match.is_match(""))
1213
}
14+
#[test]
15+
fn check_new() {
16+
let check_match = human_regex::HumanRegex::new();
17+
assert!(check_match.is_match(""))
18+
}
19+
#[test]
20+
fn check_exactly_plus_text() {
21+
let check_match = human_regex::HumanRegex::new()
22+
.begin()
23+
.exactly(4, human_regex::DIGIT)
24+
.text("-")
25+
.exactly(2, human_regex::DIGIT)
26+
.text("-")
27+
.exactly(2, human_regex::DIGIT)
28+
.end();
29+
assert!(check_match.is_match("2014-01-01"))
30+
}
1331
}

0 commit comments

Comments
 (0)