1#[cfg(feature = "csv_output")]
2use csv::Error as CsvError;
3use serde_json::Error as SerdeError;
4use std::error::Error as StdError;
5use std::fmt;
6use std::io;
7use std::path::PathBuf;
8
9#[allow(clippy::enum_variant_names)]
10#[derive(Debug)]
11pub enum Error {
12 AccessError {
13 path: PathBuf,
14 inner: io::Error,
15 },
16 CopyError {
17 from: PathBuf,
18 to: PathBuf,
19 inner: io::Error,
20 },
21 SerdeError {
22 path: PathBuf,
23 inner: SerdeError,
24 },
25 #[cfg(feature = "csv_output")]
26 /// This API requires the following crate features to be activated: csv_output
27 CsvError(CsvError),
28}
29impl fmt::Display for Error {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Error::AccessError { path, inner } => {
33 write!(f, "Failed to access file {:?}: {}", path, inner)
34 }
35 Error::CopyError { from, to, inner } => {
36 write!(f, "Failed to copy file {:?} to {:?}: {}", from, to, inner)
37 }
38 Error::SerdeError { path, inner } => write!(
39 f,
40 "Failed to read or write file {:?} due to serialization error: {}",
41 path, inner
42 ),
43 #[cfg(feature = "csv_output")]
44 Error::CsvError(inner) => write!(f, "CSV error: {}", inner),
45 }
46 }
47}
48impl StdError for Error {
49 fn description(&self) -> &str {
50 match self {
51 Error::AccessError { .. } => "AccessError",
52 Error::CopyError { .. } => "CopyError",
53 Error::SerdeError { .. } => "SerdeError",
54 #[cfg(feature = "csv_output")]
55 Error::CsvError(_) => "CsvError",
56 }
57 }
58
59 fn cause(&self) -> Option<&dyn StdError> {
60 match self {
61 Error::AccessError { inner, .. } => Some(inner),
62 Error::CopyError { inner, .. } => Some(inner),
63 Error::SerdeError { inner, .. } => Some(inner),
64 #[cfg(feature = "csv_output")]
65 Error::CsvError(inner) => Some(inner),
66 }
67 }
68}
69
70#[cfg(feature = "csv_output")]
71impl From<CsvError> for Error {
72 fn from(other: CsvError) -> Error {
73 Error::CsvError(other)
74 }
75}
76
77pub type Result<T> = ::std::result::Result<T, Error>;
78
79pub(crate) fn log_error(e: &Error) {
80 error!("error: {}", e);
81}
82