Skip to content

Commit

Permalink
feature: move regex pattern to constant module
Browse files Browse the repository at this point in the history
  • Loading branch information
Ando committed Aug 19, 2023
1 parent 99c877b commit cb39076
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub mod handler {
pub mod params {
use regex::Regex;

use crate::constant;

pub struct Params {}

impl Params {
Expand All @@ -134,7 +136,7 @@ pub mod params {
}

pub fn is_valid_local_host(host: &str) -> bool {
let pattern = r"^(localhost|127\.0\.0\.1)$";
let pattern = constant::HOST_REGEX;

match Regex::new(pattern) {
Ok(re) => re.is_match(host),
Expand All @@ -143,7 +145,7 @@ pub mod params {
}

pub fn is_valid_port(port: u32) -> bool {
let pattern = r"^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$";
let pattern = constant::PORT_REGEX;

match Regex::new(pattern) {
Ok(re) => re.is_match(&port.to_string()),
Expand All @@ -152,3 +154,8 @@ pub mod params {
}
}
}

pub mod constant {
pub const HOST_REGEX: &str = r"^(localhost|127\.0\.0\.1)$";
pub const PORT_REGEX: &str = r"^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$";
}

0 comments on commit cb39076

Please sign in to comment.