implement_error_with_kind

Macro implement_error_with_kind 

Source
macro_rules! implement_error_with_kind {
    ($err:ident, $t: path, $kind: path) => { ... };
}
Expand description

Implement the From trait for an struct with an specific structure, with ErrorKind, and message attributes.

§Params

implement_in_error_in_struct($struct_error, $error_type, $error_kind);

§Example

use heimdall_errors::implement_error_with_kind;
use std::io;

pub enum ErrorKind {
    IO(io::ErrorKind),
}

pub struct StructError {
    kind: ErrorKind,
    message: String,
}
// Implement From<io::Error> for StructError.
implement_error_with_kind!(StructError, io::Error, ErrorKind::IO);

§Code generated

The code

implement_error_with_kind!(StructError, io::Error, ErrorKind::IO);

generates the next code

 impl From<io::Error> for StructError {
    fn from(err: io::Error) -> Self {
        Self {
            kind: ErrorKind::Io(err.kind()),
            message: err.to_string(),
        }
     }
 }