1use std::fmt;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12 #[error("object backend error: {0}")]
14 Backend(String),
15
16 #[error("cryptographic error: {0}")]
18 Crypto(String),
19
20 #[error("invalid object header: {0}")]
22 InvalidHeader(String),
23
24 #[error("unsupported object format version: {0}")]
26 UnsupportedVersion(u16),
27
28 #[error("unsupported cipher suite: {0}")]
30 UnsupportedSuite(u16),
31
32 #[error("unknown key id: {0}")]
34 UnknownKey(String),
35
36 #[error("authentication failed")]
38 AuthenticationFailed,
39
40 #[error("I/O error: {0}")]
42 Io(#[from] std::io::Error),
43
44 #[error("invalid configuration: {0}")]
46 Config(String),
47}
48
49impl Error {
50 #[cfg(feature = "store")]
51 pub(crate) fn backend(err: impl fmt::Display) -> Self {
52 Self::Backend(err.to_string())
53 }
54
55 pub(crate) fn crypto(err: impl fmt::Display) -> Self {
56 Self::Crypto(err.to_string())
57 }
58
59 pub(crate) fn invalid_header(err: impl fmt::Display) -> Self {
60 Self::InvalidHeader(err.to_string())
61 }
62
63 pub(crate) fn config(err: impl fmt::Display) -> Self {
64 Self::Config(err.to_string())
65 }
66}
67
68#[cfg(feature = "store")]
70pub(crate) fn io_error(err: Error) -> std::io::Error {
71 std::io::Error::new(std::io::ErrorKind::InvalidData, err)
72}
73
74#[cfg(feature = "store")]
76pub(crate) fn error_from_io(err: std::io::Error) -> Error {
77 match err.downcast::<Error>() {
78 Ok(inner) => inner,
79 Err(err) => Error::Io(err),
80 }
81}