aboutsummaryrefslogtreecommitdiff
path: root/utils/src/error.rs
blob: f25bc5dc4ea11dd92d66a9318e5e6a99b36735c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::io;

pub trait IntoIOError {
    fn ioerr(self) -> io::Error;
    fn perror(self, parent: io::Error) -> io::Error;
}

// This impl bound is taken directly from the io::Error::new function.
impl<E: Into<Box<dyn std::error::Error + Send + Sync>>> IntoIOError for E {
    fn ioerr(self) -> io::Error {
        io::Error::new(io::ErrorKind::Other, self)
    }

    fn perror(self, parent: io::Error) -> io::Error {
        io::Error::new(parent.kind(), format!("{}: {}", self.into(), parent))
    }
}

pub trait IntoIOResult<T> {
    fn iores(self) -> io::Result<T>;
}

impl<T, E: IntoIOError> IntoIOResult<T> for Result<T, E> {
    fn iores(self) -> io::Result<T> {
        self.map_err(|e| e.ioerr())
    }
}