use crate::Error;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Category {
BadRequest,
#[non_exhaustive]
Custom {
#[cfg(feature = "http")]
http_status: u16,
#[cfg(feature = "json_rpc")]
json_rpc_status: i32,
},
}
pub trait CategoryExt {
type Ret;
fn _internal_error_mut(self, f: impl FnOnce(&mut Error)) -> Self::Ret;
fn with_category(self, category: Category) -> Self::Ret
where
Self: Sized,
{
self._internal_error_mut(|e| e.category = Some(category))
}
fn bad_request(self) -> Self::Ret
where
Self: Sized,
{
self.with_category(Category::BadRequest)
}
}
impl<T, E> CategoryExt for Result<T, E>
where
E: Into<Error>,
{
type Ret = Result<T, Error>;
fn _internal_error_mut(self, f: impl FnOnce(&mut Error)) -> Self::Ret {
match self {
Ok(t) => Ok(t),
Err(e) => {
let mut e = e.into();
f(&mut e);
Err(e)
}
}
}
}