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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
use std::fmt::{self, Display, Formatter}; use shellexpand::LookupError; pub type Result<T> = std::result::Result<T, HorustError>; #[derive(Debug)] pub enum ErrorKind { Io(std::io::Error), SerDe(toml::de::Error), NullError(std::ffi::NulError), Nix(nix::Error), ValidationError(Vec<ValidationError>), ShellExpandError(LookupError<std::env::VarError>), } #[derive(Debug)] pub struct HorustError { kind: ErrorKind, } impl Display for HorustError { fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> { match &self.kind { ErrorKind::Io(error) => write!(f, "IoError: {}", error), ErrorKind::Nix(error) => write!(f, "NixError: {}", error), ErrorKind::NullError(error) => write!(f, "NullError: {}", error), ErrorKind::SerDe(error) => write!(f, "Deserialization error(Serde): {}", error), ErrorKind::ValidationError(error) => write!(f, "ValidationErrors: {:?}", error), ErrorKind::ShellExpandError(error) => write!(f, "env expansion error: {:?}", error), } } } impl std::error::Error for HorustError {} impl From<ErrorKind> for HorustError { fn from(kind: ErrorKind) -> HorustError { HorustError { kind } } } impl From<toml::de::Error> for HorustError { fn from(err: toml::de::Error) -> Self { HorustError { kind: ErrorKind::SerDe(err), } } } impl From<std::io::Error> for HorustError { fn from(err: std::io::Error) -> Self { HorustError { kind: ErrorKind::Io(err), } } } impl From<nix::Error> for HorustError { fn from(err: nix::Error) -> Self { HorustError { kind: ErrorKind::Nix(err), } } } impl From<std::ffi::NulError> for HorustError { fn from(err: std::ffi::NulError) -> Self { HorustError { kind: ErrorKind::NullError(err), } } } impl From<Vec<ValidationError>> for HorustError { fn from(err: Vec<ValidationError>) -> Self { HorustError { kind: ErrorKind::ValidationError(err), } } } impl From<shellexpand::LookupError<std::env::VarError>> for HorustError { fn from(err: shellexpand::LookupError<std::env::VarError>) -> Self { HorustError { kind: ErrorKind::ShellExpandError(err), } } } #[derive(Debug)] pub struct ValidationError { kind: ValidationErrorKind, context: String, } #[derive(Debug)] pub enum ValidationErrorKind { MissingDependency, CommandEmpty, } impl std::error::Error for ValidationError {} impl ValidationError { pub fn new(context: &str, kind: ValidationErrorKind) -> Self { Self { context: context.to_string(), kind, } } } impl fmt::Display for ValidationError { fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), fmt::Error> { write!(f, "{}", self.context) } }