use std::{fmt::Display, error::Error}; use arrayvec::{ArrayString, CapacityError}; use fancy_regex::Regex; use lazy_static::lazy_static; use sedregex::find_and_replace; #[derive(Debug)] pub enum SedError { Capacity(CapacityError), Regex(fancy_regex::Error), SedRegex(sedregex::ErrorKind) } impl Display for SedError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Capacity(e) => e.fmt(f), Self::Regex(e) => e.fmt(f), Self::SedRegex(e) => e.fmt(f), } } } impl Error for SedError {} impl From> for SedError { fn from(e: CapacityError) -> Self { Self::Capacity(e.simplify()) } } impl From for SedError { fn from(e: fancy_regex::Error) -> Self { Self::Regex(e) } } impl From for SedError { fn from(e: sedregex::ErrorKind) -> Self { Self::SedRegex(e) } } type SedResult = Result>, SedError>; pub fn resolve(prev_msg: &str, cmd: &str) -> SedResult { lazy_static! { static ref RE: Regex = Regex::new(r"^s/.*/.*").unwrap(); // yes this regex is valid, don't worry about it } if RE.is_match(cmd)? { return if let Some(mat) = RE.find(cmd)? { let slice = &cmd[mat.start()..mat.end()]; let formatted = find_and_replace(&prev_msg, [slice])?; Ok(Some(ArrayString::from(&formatted)?)) } else { Ok(None) } } Ok(None) }