1use std::path::PathBuf;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6#[error("{source}\n\n {}", context.join("\n "))]
7pub struct InternalError {
8 source: Box<dyn std::error::Error + Send + Sync>,
9 context: Vec<String>,
10}
11
12pub trait Internal<T> {
13 fn to_internal(self) -> InternalResult<T>;
14 fn with_context<F: FnOnce() -> String>(self, f: F) -> InternalResult<T>;
15}
16
17impl<T, E: 'static + std::error::Error + Send + Sync> Internal<T> for std::result::Result<T, E> {
18 fn to_internal(self) -> InternalResult<T> {
19 self.map_err(|e| InternalError {
20 source: Box::new(e),
21 context: Vec::new(),
22 })
23 }
24
25 fn with_context<F: FnOnce() -> String>(self, f: F) -> InternalResult<T> {
26 self.map_err(|e| InternalError {
27 source: Box::new(e),
28 context: vec![f()],
29 })
30 }
31}
32
33#[derive(Error, Debug)]
35pub enum Error {
36 #[error("Entry not found for key {1:?} in cache {0:?}")]
39 EntryNotFound(PathBuf, String),
40
41 #[error("Size check failed.\n\tWanted: {0}\n\tActual: {1}")]
43 SizeError(usize, usize),
44
45 #[error(transparent)]
47 IntegrityError {
48 #[from]
49 source: ssri::Error,
51 },
52
53 #[error(transparent)]
55 InternalError {
56 #[from]
57 source: InternalError,
59 },
60}
61
62pub type Result<T> = std::result::Result<T, Error>;
64
65pub type InternalResult<T> = std::result::Result<T, InternalError>;