rvimage_domain/
result.rs

1use std::{
2    error::Error,
3    fmt::{self, Debug, Display, Formatter},
4};
5/// This will be thrown at you if the somehting within Exmex went wrong. Ok, obviously it is not an
6/// exception, so thrown needs to be understood figuratively.
7#[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}
34/// RV Image's result type with [`RvError`](RvError) as error type.
35pub type RvResult<U> = Result<U, RvError>;
36
37/// Creates an [`RvError`](RvError) with a formatted message.
38/// ```rust
39/// # use std::error::Error;
40/// use rvimage_domain::{rverr, {result::RvError}};
41/// # fn main() -> Result<(), Box<dyn Error>> {
42/// assert_eq!(rverr!("some error {}", 1), RvError::new(format!("some error {}", 1).as_str()));
43/// # Ok(())
44/// # }
45/// ```
46#[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}