From 15e7af97e970a7e3f51fc5d9421034348efffda3 Mon Sep 17 00:00:00 2001 From: Marin Minnerly Date: Thu, 18 Jan 2024 14:33:05 -0800 Subject: [PATCH] Regex is overkill. Simplify --- src/lune/builtins/net/mod.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/lune/builtins/net/mod.rs b/src/lune/builtins/net/mod.rs index 3ce6d7b..78c3397 100644 --- a/src/lune/builtins/net/mod.rs +++ b/src/lune/builtins/net/mod.rs @@ -3,7 +3,6 @@ use std::net::Ipv4Addr; use mlua::prelude::*; use hyper::header::CONTENT_ENCODING; -use regex::Regex; use crate::lune::{scheduler::Scheduler, util::TableBuilder}; @@ -144,19 +143,17 @@ where .app_data_ref::<&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 { Some(addr) => { let addr_str = addr.to_str()?; - if let Some(caps) = address_pattern.captures(addr_str) { - caps[1].parse::()? - } else { - return Err(LuaError::runtime(format!( + addr_str + .trim_start_matches("http://") + .trim_start_matches("https://") + .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}')" - ))); - } + )))? } None => DEFAULT_IP_ADDRESS, };