Simplify error handling in sedbot
This commit is contained in:
parent
274f064d35
commit
83b88e9e43
|
@ -6,47 +6,43 @@ use lazy_static::lazy_static;
|
||||||
use sedregex::find_and_replace;
|
use sedregex::find_and_replace;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum SedErrorKind {
|
pub enum SedError {
|
||||||
Capacity(CapacityError),
|
Capacity(CapacityError),
|
||||||
Regex(fancy_regex::Error),
|
Regex(fancy_regex::Error),
|
||||||
SedRegex(sedregex::ErrorKind)
|
SedRegex(sedregex::ErrorKind)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
impl Display for SedError {
|
||||||
pub struct SedCapacityError(SedErrorKind);
|
|
||||||
|
|
||||||
impl Display for SedCapacityError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
// yeah it's ugly but there's no better way afaik
|
match self {
|
||||||
match &self.0 {
|
Self::Capacity(e) => e.fmt(f),
|
||||||
SedErrorKind::Capacity(e) => Display::fmt(e, f),
|
Self::Regex(e) => e.fmt(f),
|
||||||
SedErrorKind::Regex(e) => Display::fmt(e, f),
|
Self::SedRegex(e) => e.fmt(f),
|
||||||
SedErrorKind::SedRegex(e) => Display::fmt(e, f),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for SedCapacityError {}
|
impl Error for SedError {}
|
||||||
|
|
||||||
impl<T> From<CapacityError<T>> for SedCapacityError {
|
impl<T> From<CapacityError<T>> for SedError {
|
||||||
fn from(e: CapacityError<T>) -> Self {
|
fn from(e: CapacityError<T>) -> Self {
|
||||||
Self { 0: SedErrorKind::Capacity(e.simplify()) }
|
Self::Capacity(e.simplify())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<fancy_regex::Error> for SedCapacityError {
|
impl From<fancy_regex::Error> for SedError {
|
||||||
fn from(e: fancy_regex::Error) -> Self {
|
fn from(e: fancy_regex::Error) -> Self {
|
||||||
Self { 0: SedErrorKind::Regex(e) }
|
Self::Regex(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<sedregex::ErrorKind> for SedCapacityError {
|
impl From<sedregex::ErrorKind> for SedError {
|
||||||
fn from(e: sedregex::ErrorKind) -> Self {
|
fn from(e: sedregex::ErrorKind) -> Self {
|
||||||
Self { 0: SedErrorKind::SedRegex(e) }
|
Self::SedRegex(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type SedResult = Result<Option<ArrayString<512>>, SedCapacityError>;
|
type SedResult = Result<Option<ArrayString<512>>, SedError>;
|
||||||
|
|
||||||
pub fn resolve(prev_msg: &str, cmd: &str) -> SedResult {
|
pub fn resolve(prev_msg: &str, cmd: &str) -> SedResult {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -54,14 +50,12 @@ pub fn resolve(prev_msg: &str, cmd: &str) -> SedResult {
|
||||||
}
|
}
|
||||||
|
|
||||||
if RE.is_match(cmd)? {
|
if RE.is_match(cmd)? {
|
||||||
if let Some(mat) = RE.find(cmd)? {
|
return if let Some(mat) = RE.find(cmd)? {
|
||||||
let slice = &cmd[mat.start()..mat.end()];
|
let slice = &cmd[mat.start()..mat.end()];
|
||||||
|
|
||||||
let formatted = find_and_replace(&prev_msg, [slice])?;
|
let formatted = find_and_replace(&prev_msg, [slice])?;
|
||||||
|
Ok(Some(ArrayString::from(&formatted)?))
|
||||||
return Ok(Some(ArrayString::from(&formatted)?));
|
|
||||||
} else {
|
} else {
|
||||||
return Ok(None);
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue