I am wrapping errors per this Rust By Example page: https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/wrap_error.html
Currently my code contains a lot of boilerplate:
#[derive(Debug)]
pub enum Error {
JsonError(serde_json::Error),
YamlError(serde_yaml::Error),
IOError(String),
// ...
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::JsonError(ref e) => e.fmt(f),
Error::YamlError(ref e) => e.fmt(f),
Error::IOError(ref e) => e.fmt(f),
// ...
}
}
}
impl From<serde_yaml::Error> for Error {
fn from(err: serde_yaml::Error) -> Error {
Error::YamlError(err)
}
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
Error::JsonError(err)
}
}
// ...
Is there a better way to write this? It's much longer with all seven error types.