macro_rules! define_category {
    (
        $(#[$docs:meta])*
        pub enum $name:ident {
            $(
                #[doc = $msg:literal]
                $ident:ident = $code:literal,
             )+
        }
    ) => { ... };
}
Expand description

Defines error code Category as enum which implements conversion into generic ErrorCode

This enum shall implement following traits:

  • Clone
  • Copy
  • Debug
  • Display - uses ErrorCode fmt::Display
  • PartialEq / Eq
  • PartialOrd / Ord

§Usage

use error_code::{define_category, ErrorCode};

define_category!(
    ///This is documentation for my error
    ///
    ///Documentation of variants only allow 1 line comment and it should be within 256 characters
    pub enum MyError {
        ///Success
        Success = 0,
        ///This is bad
        Error = 1,
    }
);

fn handle_error(res: Result<(), MyError>) -> Result<(), ErrorCode> {
    res?;
    Ok(())
}

let error = handle_error(Err(MyError::Error)).expect_err("Should return error");
assert_eq!(error.to_string(), "MyError(1): This is bad");
assert_eq!(error.to_string(), MyError::Error.to_string());