Skip to main content

riptering/
error.rs

1/// Errors produced by riptering cryptographic operations.
2#[derive(Debug, thiserror::Error)]
3pub enum Error {
4    #[error("cryptographic operation failed: {0}")]
5    Crypto(String),
6
7    #[error("{backend} does not support {operation:?} ({algorithm})")]
8    UnsupportedAlgorithm {
9        backend: crate::backend::BackendId,
10        operation: crate::backend::Operation,
11        algorithm: String,
12    },
13
14    #[error("cryptographic backend {backend} has not been explicitly initialized")]
15    BackendNotInitialized { backend: crate::backend::BackendId },
16
17    #[error("failed to initialize cryptographic backend {backend}: {message}")]
18    BackendInitialization {
19        backend: crate::backend::BackendId,
20        message: String,
21    },
22
23    #[error("FIPS mode is unavailable for {backend}: {message}")]
24    FipsUnavailable {
25        backend: crate::backend::BackendId,
26        message: String,
27    },
28
29    #[error("key error: {0}")]
30    Key(String),
31
32    #[error("I/O error: {0}")]
33    Io(#[from] std::io::Error),
34
35    #[cfg(all(feature = "pkcs11", not(target_arch = "wasm32")))]
36    #[error("PKCS#11 error: {0}")]
37    Pkcs11(String),
38}
39
40impl Error {
41    /// Construct a deterministic unsupported-operation error for the selected provider.
42    pub fn unsupported(operation: crate::backend::Operation, algorithm: impl Into<String>) -> Self {
43        Self::UnsupportedAlgorithm {
44            backend: crate::backend::selected_backend(),
45            operation,
46            algorithm: algorithm.into(),
47        }
48    }
49}
50
51pub type Result<T> = std::result::Result<T, Error>;