| 1 | use std::error::Error; |
| 2 | use std::fmt; |
| 3 | |
| 4 | #[derive(PartialEq, Eq, Copy, Clone, Debug)] |
| 5 | pub enum ErrorKind { |
| 6 | SysError, |
| 7 | } |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | enum ErrorRepr { |
| 11 | FromNix(nix::Error), |
| 12 | WithDescription(ErrorKind, &'static str), |
| 13 | } |
| 14 | |
| 15 | #[derive(Debug)] |
| 16 | pub struct PrivDropError { |
| 17 | repr: ErrorRepr, |
| 18 | } |
| 19 | |
| 20 | impl Error for PrivDropError { |
| 21 | fn cause(&self) -> Option<&dyn Error> { |
| 22 | match self.repr { |
| 23 | ErrorRepr::FromNix(ref e) => Some(e as &dyn Error), |
| 24 | _ => None, |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl fmt::Display for PrivDropError { |
| 30 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { |
| 31 | match self.repr { |
| 32 | ErrorRepr::FromNix(ref e) => e.fmt(f), |
| 33 | ErrorRepr::WithDescription(_, description) => description.fmt(f), |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl From<nix::Error> for PrivDropError { |
| 39 | fn from(e: nix::Error) -> PrivDropError { |
| 40 | PrivDropError { |
| 41 | repr: ErrorRepr::FromNix(e), |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl From<(ErrorKind, &'static str)> for PrivDropError { |
| 47 | fn from((kind, description): (ErrorKind, &'static str)) -> PrivDropError { |
| 48 | PrivDropError { |
| 49 | repr: ErrorRepr::WithDescription(kind, description), |
| 50 | } |
| 51 | } |
| 52 | } |