Skip to content

Commit

Permalink
changed lazy_static and Regex to lazy_regex
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jun 23, 2022
1 parent 2f815b3 commit 066feee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ repository = "https://github.com/veeso/tui-realm"
[dependencies]
bitflags = "^1.3"
crossterm = { version = "^0.23", optional = true }
lazy_static = "^1.0.0"
regex = "^1.5.0"
lazy-regex = "^2.3.0"
serde = { version = "^1", features = [ "derive" ], optional = true }
termion = { version = "^1.5", optional = true }
thiserror = "^1.0.0"
Expand Down
6 changes: 2 additions & 4 deletions src/core/props/input_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ mod test {

use super::*;

use lazy_regex::{Lazy, Regex};
use pretty_assertions::assert_eq;
use regex::Regex;

#[test]
fn validate_input_type() {
Expand Down Expand Up @@ -301,9 +301,7 @@ mod test {
}

fn custom_valid(s: &str) -> bool {
lazy_static! {
static ref TEST_REGEX: Regex = Regex::new(r".*(:?[0-9]\.[0-9]\.[0-9])").unwrap();
}
static TEST_REGEX: Lazy<Regex> = lazy_regex!(r".*(:?[0-9]\.[0-9]\.[0-9])");
TEST_REGEX.is_match(s)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
)]

#[macro_use]
extern crate lazy_static;
extern crate lazy_regex;
extern crate self as tuirealm;
extern crate tui as tuirs;
#[cfg(feature = "derive")]
Expand Down
73 changes: 33 additions & 40 deletions src/utils/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@
use super::{Email, PhoneNumber};
use crate::tui::style::Color;

use regex::Regex;
use lazy_regex::{Lazy, Regex};
use std::str::FromStr;
/**
* Regex matches:
* - group 1: Red
* - group 2: Green
* - group 3: Blue
*/
static COLOR_HEX_REGEX: Lazy<Regex> =
lazy_regex!(r"#(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})");
/**
* Regex matches:
* - group 2: Red
* - group 4: Green
* - group 6: blue
*/
static COLOR_RGB_REGEX: Lazy<Regex> = lazy_regex!(
r"^(rgb)?\(?([01]?\d\d?|2[0-4]\d|25[0-5])(\W+)([01]?\d\d?|2[0-4]\d|25[0-5])\W+(([01]?\d\d?|2[0-4]\d|25[0-5])\)?)"
);

lazy_static! {
/**
* Regex matches:
* - group 1: Red
* - group 2: Green
* - group 3: Blue
*/
static ref COLOR_HEX_REGEX: Regex = Regex::new(r"#(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})(:?[0-9a-fA-F]{2})").unwrap();
/**
* Regex matches:
* - group 2: Red
* - group 4: Green
* - group 6: blue
*/
static ref COLOR_RGB_REGEX: Regex = Regex::new(r"^(rgb)?\(?([01]?\d\d?|2[0-4]\d|25[0-5])(\W+)([01]?\d\d?|2[0-4]\d|25[0-5])\W+(([01]?\d\d?|2[0-4]\d|25[0-5])\)?)").unwrap();
/**
* Regex matches:
* - group 1: name
* - group 2: mail agent
*/
static EMAIL_REGEX: Lazy<Regex> = lazy_regex!(
r"^(:?[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+)@(:?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$)"
);

/**
* Regex matches:
* - group 1: name
* - group 2: mail agent
*/
static ref EMAIL_REGEX: Regex = Regex::new(r"^(:?[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+)@(:?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$)").unwrap();
/**
* Regex matches:
* - group 2|3: prefix
* - group 4: number
*/
static PHONE_NUMBER_REGEX: Lazy<Regex> =
lazy_regex!(r"^([+]{1}(:?[0-9]{1,4})|[0]{2}(:?[0-9]{1,4}))?(:?[-\s\./0-9]*$)");

/**
* Regex matches:
* - group 2|3: prefix
* - group 4: number
*/
static ref PHONE_NUMBER_REGEX: Regex = Regex::new(r"^([+]{1}(:?[0-9]{1,4})|[0]{2}(:?[0-9]{1,4}))?(:?[-\s\./0-9]*$)").unwrap();
}

/// ### parse_email
///
/// If provided string is a valid email address, returns the name and the mail agent
///
/// ```rust
/// use tuirealm::utils::parser::*;
/// use tuirealm::utils::Email;
Expand All @@ -60,11 +60,8 @@ pub fn parse_email(s: &str) -> Option<Email> {
}
}

/// ### parse_phone_number
///
/// If provided string is a valid phone number address, returns the prefix (if any) and the number
///
/// ```rust
/// use tuirealm::utils::parser::*;
/// use tuirealm::utils::PhoneNumber;
Expand All @@ -85,8 +82,6 @@ pub fn parse_phone_number(s: &str) -> Option<PhoneNumber> {
}
}

/// ### parse_color
///
/// Parse color from string into a `Color` enum.
///
/// Color may be in different format:
Expand Down Expand Up @@ -299,8 +294,6 @@ fn parse_hex_color(color: &str) -> Option<Color> {
})
}

/// ### parse_rgb_color
///
/// Try to parse a color in rgb format, such as:
///
/// - "rgb(255, 64, 32)"
Expand Down

0 comments on commit 066feee

Please sign in to comment.