Macro nes::define_error [] [src]

macro_rules! define_error {
    ( $error_name:ident,
        $(
            $var_name:ident ( $( $field_name:ident : $field_type:ty ),* ) => $message:expr
        ),*
    ) => { ... };
}

This macro defines the error.

# Example

define_error!( ReadFileError,
    IOError(io_error:Box<std::io::Error>) =>
        "IO Error: {}",
    ReadFileError(io_error:Box<std::io::Error>, file:String ) =>
        "Can not read file \"{2}\" : {1}" //1,2 is order of args, note:0 is ErrorInfo
);

define_error!( CommonError,
    ReadFileError(read_file_error:Box<ReadFileError>) =>
        "read file error {}",
    NoArguments() =>
        "no arguments",
    IncorrectExtension(file_name:String, extension:String) =>
        "Expected extension \"{2}\" for file \"{1}\""
);

You must push other errors in Box. This prevent results that have large size or infinite(if error is recursive). In this case Box<..> must be written first, and may be accessed by index like {2}, but index 0 has ErrorInfo, that describes where the error has been occurred.

This macro generates code like

pub enum ReadFileError {
    IOError(ErrorInfo, Box<std::io::Error>),
    ReadFileError(ErrorInfo, Box<std::io::Error>, String )
);

impl ReadFileError {
    pub fn get_error_info(&mut self) -> &ErrorInfo { ... }
}

impl std::fmt::Display for ReadFileError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ReadFileError::ReadFileError(ref error_info, ref io_error, ref file) => write!(f, "{}\n,Can not read file \"{2}\" : {1}", error_info, io_error, file),
        }
    }
}

impl std::fmt::Debug for ReadFileError { ... } //Short description.