1use std::{
2 error::Error,
3 fmt::{self, Debug, Display, Formatter},
4};
5#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
8pub struct RvError {
9 msg: String,
10}
11impl RvError {
12 #[must_use]
13 pub fn new(msg: &str) -> RvError {
14 RvError {
15 msg: msg.to_string(),
16 }
17 }
18 #[must_use]
19 pub fn msg(&self) -> &str {
20 &self.msg
21 }
22}
23impl Display for RvError {
24 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
25 write!(f, "{}", self.msg)
26 }
27}
28impl Error for RvError {}
29impl From<&str> for RvError {
30 fn from(value: &str) -> Self {
31 RvError::new(value)
32 }
33}
34pub type RvResult<U> = Result<U, RvError>;
36
37#[macro_export]
47macro_rules! rverr {
48 ($s:literal) => {
49 $crate::result::RvError::new(format!($s).as_str())
50 };
51 ($s:literal, $( $exps:expr ),*) => {
52 $crate::result::RvError::new(format!($s, $($exps,)*).as_str())
53 }
54}
55
56pub fn to_rv<E: Debug>(e: E) -> RvError {
57 rverr!(
58 "original error type is '{:?}', error message is '{:?}'",
59 std::any::type_name::<E>(),
60 e
61 )
62}