diff options
author | Martin Nordholts <enselic@gmail.com> | 2021-08-26 13:12:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-26 13:12:21 +0200 |
commit | 19c3e82abf70324dcf9d1c310aed7881f44fd35f (patch) | |
tree | 702a8302f6274bdd2dbd37947198d2b2ba8f0e5d /src/error.rs | |
parent | f1c0fd7343c6c4c6a5ec198e533a6e8de3369472 (diff) |
Replace deprecated 'error-chain' with 'thiserror' (#1820)
We can't use #[from] on Error::Msg(String) because String does not implement Error.
(Which it shouldn't; see e.g. https://internals.rust-lang.org/t/impl-error-for-string/8881.)
So we implement From manually for Error::Msg, since our current code was written
in that way for error-chain.
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/src/error.rs b/src/error.rs index 0448fd07..54f460e7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,42 +1,52 @@ -use error_chain::error_chain; use std::io::Write; +use thiserror::Error; -error_chain! { - foreign_links { - Clap(::clap::Error) #[cfg(feature = "minimal-application")]; - Io(::std::io::Error); - SyntectError(::syntect::LoadingError); - ParseIntError(::std::num::ParseIntError); - GlobParsingError(::globset::Error); - SerdeYamlError(::serde_yaml::Error); +#[derive(Error, Debug)] +pub enum Error { + #[error(transparent)] + Io(#[from] ::std::io::Error), + #[error(transparent)] + SyntectError(#[from] ::syntect::LoadingError), + #[error(transparent)] + ParseIntError(#[from] ::std::num::ParseIntError), + #[error(transparent)] + GlobParsingError(#[from] ::globset::Error), + #[error(transparent)] + SerdeYamlError(#[from] ::serde_yaml::Error), + #[error("unable to detect syntax for {0}")] + UndetectedSyntax(String), + #[error("unknown syntax: '{0}'")] + UnknownSyntax(String), + #[error("Unknown style '{0}'")] + UnknownStyle(String), + #[error("Use of bat as a pager is disallowed in order to avoid infinite recursion problems")] + InvalidPagerValueBat, + #[error("{0}")] + Msg(String), +} + +impl From<&'static str> for Error { + fn from(s: &'static str) -> Self { + Error::Msg(s.to_owned()) } +} - errors { - UndetectedSyntax(input: String) { - description("unable to detect syntax"), - display("unable to detect syntax for {}", input) - } - UnknownSyntax(name: String) { - description("unknown syntax"), - display("unknown syntax: '{}'", name) - } - InvalidPagerValueBat { - description("invalid value `bat` for pager property"), - display("Use of bat as a pager is disallowed in order to avoid infinite recursion problems") - } +impl From<String> for Error { + fn from(s: String) -> Self { + Error::Msg(s) } } +pub type Result<T> = std::result::Result<T, Error>; + pub fn default_error_handler(error: &Error, output: &mut dyn Write) { use ansi_term::Colour::Red; match error { - Error(ErrorKind::Io(ref io_error), _) - if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => - { + Error::Io(ref io_error) if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => { ::std::process::exit(0); } - Error(ErrorKind::SerdeYamlError(_), _) => { + Error::SerdeYamlError(_) => { writeln!( output, "{}: Error while parsing metadata.yaml file: {}", |