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
use std::path::PathBuf;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("{source}\n\n {}", context.join("\n "))]
pub struct InternalError {
source: Box<dyn std::error::Error + Send + Sync>,
context: Vec<String>,
}
pub trait Internal<T> {
fn to_internal(self) -> InternalResult<T>;
fn with_context<F: FnOnce() -> String>(self, f: F) -> InternalResult<T>;
}
impl<T, E: 'static + std::error::Error + Send + Sync> Internal<T> for std::result::Result<T, E> {
fn to_internal(self) -> InternalResult<T> {
self.map_err(|e| InternalError {
source: Box::new(e),
context: Vec::new(),
})
}
fn with_context<F: FnOnce() -> String>(self, f: F) -> InternalResult<T> {
self.map_err(|e| InternalError {
source: Box::new(e),
context: vec![f()],
})
}
}
#[derive(Error, Debug)]
pub enum Error {
#[error("Entry not found for key {1:?} in cache {0:?}")]
EntryNotFound(PathBuf, String),
#[error("Size check failed.\n\tWanted: {0}\n\tActual: {1}")]
SizeError(usize, usize),
#[error(transparent)]
IntegrityError {
#[from]
source: ssri::Error,
},
#[error(transparent)]
InternalError {
#[from]
source: InternalError,
},
}
pub type Result<T> = std::result::Result<T, Error>;
pub type InternalResult<T> = std::result::Result<T, InternalError>;