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