airs_types/errors/
mod.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display, Formatter},
4    path::{Path, PathBuf},
5};
6
7pub type AirsResult<T = ()> = Result<T, AirsErrorKind>;
8
9#[derive(Debug)]
10pub struct AirsError {
11    pub kind: Box<AirsErrorKind>,
12}
13
14#[derive(Debug)]
15pub enum AirsErrorKind {
16    IoError { path: PathBuf, raw: std::io::Error },
17}
18
19impl Error for AirsError {}
20
21impl Display for AirsError {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        Display::fmt(&self.kind, f)
24    }
25}
26impl Display for AirsErrorKind {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        match self {
29            AirsErrorKind::IoError { path, raw } => {
30                write!(f, "io error: {} ({:?})", raw, path)
31            }
32        }
33    }
34}
35
36impl From<std::io::Error> for AirsErrorKind {
37    fn from(raw: std::io::Error) -> Self {
38        Self::IoError { path: PathBuf::new(), raw }
39    }
40}
41
42impl AirsError {
43    pub fn set_path<P: AsRef<Path>>(&mut self, new: P) {
44        match self.kind.as_mut() {
45            AirsErrorKind::IoError { path, .. } => {
46                *path = new.as_ref().to_path_buf();
47            }
48        }
49    }
50    pub fn with_path<P: AsRef<Path>>(mut self, path: P) -> Self {
51        self.set_path(path);
52        self
53    }
54}