Regex is overkill. Simplify

This commit is contained in:
Marin Minnerly 2024-01-18 14:33:05 -08:00
parent 29dc42d435
commit 15e7af97e9

View file

@ -3,7 +3,6 @@ use std::net::Ipv4Addr;
use mlua::prelude::*; use mlua::prelude::*;
use hyper::header::CONTENT_ENCODING; use hyper::header::CONTENT_ENCODING;
use regex::Regex;
use crate::lune::{scheduler::Scheduler, util::TableBuilder}; use crate::lune::{scheduler::Scheduler, util::TableBuilder};
@ -144,19 +143,17 @@ where
.app_data_ref::<&Scheduler>() .app_data_ref::<&Scheduler>()
.expect("Lua struct is missing scheduler"); .expect("Lua struct is missing scheduler");
let address_pattern = Regex::new(r"(?:.*:\/\/)?(\d+\.\d+\.\d+\.\d+)(?::\d+)?").unwrap();
let address: Ipv4Addr = match &config.address { let address: Ipv4Addr = match &config.address {
Some(addr) => { Some(addr) => {
let addr_str = addr.to_str()?; let addr_str = addr.to_str()?;
if let Some(caps) = address_pattern.captures(addr_str) { addr_str
caps[1].parse::<Ipv4Addr>()? .trim_start_matches("http://")
} else { .trim_start_matches("https://")
return Err(LuaError::runtime(format!( .parse()
.map_err(|_e| LuaError::RuntimeError(format!(
"IP address format is incorrect (expected an IP in the form 'http://0.0.0.0' or '0.0.0.0', got '{addr_str}')" "IP address format is incorrect (expected an IP in the form 'http://0.0.0.0' or '0.0.0.0', got '{addr_str}')"
))); )))?
}
} }
None => DEFAULT_IP_ADDRESS, None => DEFAULT_IP_ADDRESS,
}; };