use std::path::PathBuf; use thiserror::Error; #[derive(Error, Debug)] #[error("{source}\n\n {}", context.join("\n "))] pub struct InternalError { source: Box, context: Vec, } pub trait Internal { fn to_internal(self) -> InternalResult; fn with_context String>(self, f: F) -> InternalResult; } impl Internal for std::result::Result { fn to_internal(self) -> InternalResult { self.map_err(|e| InternalError { source: Box::new(e), context: Vec::new(), }) } fn with_context String>(self, f: F) -> InternalResult { self.map_err(|e| InternalError { source: Box::new(e), context: vec![f()], }) } } /// Error type returned by all API calls. #[derive(Error, Debug)] pub enum Error { /// Returned when an index entry could not be found during /// lookup. #[error("Entry not found for key {1:?} in cache {0:?}")] EntryNotFound(PathBuf, String), /// Returned when a size check has failed. #[error("Size check failed.\n\tWanted: {0}\n\tActual: {1}")] SizeError(usize, usize), /// Returned when an integrity check has failed. #[error(transparent)] IntegrityError { #[from] /// The underlying error source: ssri::Error, }, /// Returned if an internal (e.g. io) operation has failed. #[error(transparent)] InternalError { #[from] /// The underlying error source: InternalError, }, } /// The result type returned by calls to this library pub type Result = std::result::Result; pub type InternalResult = std::result::Result;