1use std::io::Write;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5#[non_exhaustive]
6pub enum Error {
7 #[error(transparent)]
8 Io(#[from] ::std::io::Error),
9 #[error(transparent)]
10 SyntectError(#[from] ::syntect::Error),
11 #[error(transparent)]
12 SyntectLoadingError(#[from] ::syntect::LoadingError),
13 #[error(transparent)]
14 ParseIntError(#[from] ::std::num::ParseIntError),
15 #[error(transparent)]
16 GlobParsingError(#[from] ::globset::Error),
17 #[error(transparent)]
18 SerdeYamlError(#[from] ::serde_yaml::Error),
19 #[error("unable to detect syntax for {0}")]
20 UndetectedSyntax(String),
21 #[error("unknown syntax: '{0}'")]
22 UnknownSyntax(String),
23 #[error("Unknown style '{0}'")]
24 UnknownStyle(String),
25 #[error("Use of bat as a pager is disallowed in order to avoid infinite recursion problems")]
26 InvalidPagerValueBat,
27 #[error("{0}")]
28 Msg(String),
29}
30
31impl From<&'static str> for Error {
32 fn from(s: &'static str) -> Self {
33 Error::Msg(s.to_owned())
34 }
35}
36
37impl From<String> for Error {
38 fn from(s: String) -> Self {
39 Error::Msg(s)
40 }
41}
42
43pub type Result<T> = std::result::Result<T, Error>;
44
45pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
46 use ansi_term::Colour::Red;
47
48 match error {
49 Error::Io(ref io_error) if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => {
50 ::std::process::exit(0);
51 }
52 Error::SerdeYamlError(_) => {
53 writeln!(
54 output,
55 "{}: Error while parsing metadata.yaml file: {}",
56 Red.paint("[bat error]"),
57 error
58 )
59 .ok();
60 }
61 _ => {
62 writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
63 }
64 };
65}