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
use std::path::PathBuf;
use miette::Diagnostic;
use thiserror::Error;
#[derive(Error, Debug, Diagnostic)]
pub enum Error {
#[error("Entry not found for key {1:?} in cache {0:?}")]
#[diagnostic(code(cacache::entry_not_found), url(docsrs))]
EntryNotFound(PathBuf, String),
#[error("Size check failed.\n\tWanted: {0}\n\tActual: {1}")]
#[diagnostic(code(cacache::size_mismatch), url(docsrs))]
SizeMismatch(usize, usize),
#[error("{1}")]
#[diagnostic(code(cacache::io_error), url(docsrs))]
IoError(#[source] std::io::Error, String),
#[error("{1}")]
#[diagnostic(code(cacache::serde_error), url(docsrs))]
SerdeError(#[source] serde_json::Error, String),
#[error(transparent)]
#[diagnostic(code(cacache::integrity_error), url(docsrs))]
IntegrityError(#[from] ssri::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
pub trait IoErrorExt<T> {
fn with_context<F: FnOnce() -> String>(self, f: F) -> Result<T>;
}
impl<T> IoErrorExt<T> for std::result::Result<T, std::io::Error> {
fn with_context<F: FnOnce() -> String>(self, f: F) -> Result<T> {
match self {
Ok(t) => Ok(t),
Err(e) => Err(Error::IoError(e, f())),
}
}
}
impl<T> IoErrorExt<T> for std::result::Result<T, serde_json::Error> {
fn with_context<F: FnOnce() -> String>(self, f: F) -> Result<T> {
match self {
Ok(t) => Ok(t),
Err(e) => Err(Error::SerdeError(e, f())),
}
}
}
pub fn io_error(err: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::Other, err)
}