more stuff

This commit is contained in:
Zomatree
2024-04-24 02:23:52 +01:00
parent 7a4421089c
commit 67569f932e
11 changed files with 254 additions and 79 deletions

View File

@@ -1,3 +1,5 @@
use std::panic::Location;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
@@ -172,6 +174,37 @@ macro_rules! query {
};
}
pub trait ToRevoltError<T> {
#[track_caller]
fn to_internal_error(self) -> Result<T, Error>;
}
impl<T, E> ToRevoltError<T> for Result<T, E> {
fn to_internal_error(self) -> Result<T, Error> {
self.map_err(|_| {
let loc = Location::caller();
Error {
error_type: ErrorType::InternalError,
location: format!("{}:{}:{}", loc.file(), loc.line(), loc.column())
}
})
}
}
impl<T> ToRevoltError<T> for Option<T> {
fn to_internal_error(self) -> Result<T, Error> {
self.ok_or_else(|| {
let loc = Location::caller();
Error {
error_type: ErrorType::InternalError,
location: format!("{}:{}:{}", loc.file(), loc.line(), loc.column())
}
})
}
}
#[cfg(test)]
mod tests {
use crate::ErrorType;