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
//! Cache error types.

use std::sync::Arc;

/// A result type returning cache errors.
pub type Result<T> = std::result::Result<T, Error>;

/// A result type returning reference counted cache errors.
///
/// Stores an [`Arc<Error>`] since the error will be stuck inside a
/// [`OnceCell`](once_cell::sync::OnceCell) and cannot be owned without cloning.
pub type ArcResult<T> = std::result::Result<T, Arc<Error>>;

/// The error type for cache functions.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[allow(missing_docs)]
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[allow(missing_docs)]
    #[error(transparent)]
    Transport(#[from] tonic::transport::Error),
    #[allow(missing_docs)]
    #[error(transparent)]
    Rusqlite(#[from] tokio_rusqlite::Error),
    #[allow(missing_docs)]
    #[error(transparent)]
    Toml(#[from] toml::de::Error),
    #[allow(missing_docs)]
    #[error(transparent)]
    Flexbuffers(#[from] flexbuffers::DeserializationError),
    #[allow(missing_docs)]
    #[error(transparent)]
    Rpc(#[from] tonic::Status),
    #[allow(missing_docs)]
    #[error(transparent)]
    Join(#[from] tokio::task::JoinError),
    /// The user-provided generator panicked.
    #[error("generator panicked")]
    Panic,
    /// Exponential backoff for polling the server failed.
    #[error(transparent)]
    Backoff(#[from] Box<backoff::Error<Error>>),
    /// The desired cache entry is currently being loaded.
    #[error("entry is currently being loaded")]
    EntryLoading,
    /// The desired cache entry is currently unavailable.
    #[error("entry is currently unavailable")]
    EntryUnavailable,
    /// The desired cache entry cannot be assigned.
    #[error("entry cannot be assigned")]
    EntryUnassignable,
}

/// The error type returned by [`CacheHandle::try_inner`](crate::CacheHandle::try_inner).
pub enum TryInnerError<'a, E> {
    /// An error thrown by the cache.
    CacheError(Arc<Error>),
    /// An error thrown by the generator.
    GeneratorError(&'a E),
}

impl<'a, E> From<Arc<Error>> for TryInnerError<'a, E> {
    fn from(value: Arc<Error>) -> Self {
        Self::CacheError(value)
    }
}

impl<'a, E> From<&'a E> for TryInnerError<'a, E> {
    fn from(value: &'a E) -> Self {
        Self::GeneratorError(value)
    }
}