macro_rules! map_to_error {
    ($error:ident :: $error_reason:ident) => { ... };
}
Expand description

Helper for mapping errors from one to another

Usage

Error Mapping

The explicit way

use std::fs;
use error_ex::{create_error, map_to_error};
create_error!(FileError => IoError);
create_error!(SchemaError => ParseError);
let error: Result<(), FileError::Error> = Err(FileError::IoError("".to_string()));
let mapped = error.map_err(|error| {
    SchemaError::ParseError(format!("SchemaError::ParseError error {error}"))
});

Macro error mapping

The above code can be simplified using map_to_error! macro


use std::fs;
use std::io::Error;
use error_ex::{create_error, map_to_error};
create_error!(FileError => IoError);
create_error!(SchemaError => ParseError);
let error: Result<(), FileError::Error> = Err(FileError::IoError("".to_string()));
let mapped = error.map_err(map_to_error!(SchemaError::ParseError));