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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
use nix; use std::error::Error; use std::fmt; #[derive(PartialEq, Eq, Copy, Clone, Debug)] pub enum ErrorKind { SysError, } #[derive(Debug)] enum ErrorRepr { FromNix(nix::Error), WithDescription(ErrorKind, &'static str), } #[derive(Debug)] pub struct PrivDropError { repr: ErrorRepr, } impl Error for PrivDropError { fn description(&self) -> &str { match self.repr { ErrorRepr::FromNix(ref e) => e.description(), ErrorRepr::WithDescription(_, description) => description, } } fn cause(&self) -> Option<&Error> { match self.repr { ErrorRepr::FromNix(ref e) => Some(e as &Error), _ => None, } } } impl fmt::Display for PrivDropError { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match self.repr { ErrorRepr::FromNix(ref e) => e.fmt(f), ErrorRepr::WithDescription(_, description) => description.fmt(f), } } } impl From<nix::Error> for PrivDropError { fn from(e: nix::Error) -> PrivDropError { PrivDropError { repr: ErrorRepr::FromNix(e), } } } impl From<(ErrorKind, &'static str)> for PrivDropError { fn from((kind, description): (ErrorKind, &'static str)) -> PrivDropError { PrivDropError { repr: ErrorRepr::WithDescription(kind, description), } } }