use std::{
error::Error,
fmt::{self, Debug, Display, Formatter},
};
use tracing::error;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub struct RvError {
msg: String,
}
impl RvError {
pub fn new(msg: &str) -> RvError {
RvError {
msg: msg.to_string(),
}
}
pub fn msg(&self) -> &str {
&self.msg
}
}
impl Display for RvError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
impl Error for RvError {}
impl From<&str> for RvError {
fn from(value: &str) -> Self {
RvError::new(value)
}
}
pub type RvResult<U> = Result<U, RvError>;
pub fn trace_ok<T, E>(x: Result<T, E>) -> Option<T>
where
E: Debug,
{
match x {
Ok(x) => Some(x),
Err(e) => {
error!("{e:?}");
None
}
}
}
#[macro_export]
macro_rules! rverr {
($s:literal) => {
$crate::result::RvError::new(format!($s).as_str())
};
($s:literal, $( $exps:expr ),*) => {
$crate::result::RvError::new(format!($s, $($exps,)*).as_str())
}
}
pub fn to_rv<E: Debug>(e: E) -> RvError {
rverr!(
"original error type is '{:?}', error message is '{:?}'",
std::any::type_name::<E>(),
e
)
}