Skip to main content

wasi_crypto_guest/
error.rs

1use std::convert::TryFrom;
2
3use num_enum::{IntoPrimitive, TryFromPrimitive};
4
5use crate::raw;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, TryFromPrimitive, IntoPrimitive)]
8#[repr(u16)]
9pub enum Error {
10    GuestError = raw::CRYPTO_ERRNO_GUEST_ERROR.raw(),
11    NotImplemented = raw::CRYPTO_ERRNO_NOT_IMPLEMENTED.raw(),
12    UnsupportedFeature = raw::CRYPTO_ERRNO_UNSUPPORTED_FEATURE.raw(),
13    ProhibitedOperation = raw::CRYPTO_ERRNO_PROHIBITED_OPERATION.raw(),
14    UnsupportedEncoding = raw::CRYPTO_ERRNO_UNSUPPORTED_ENCODING.raw(),
15    UnsupportedAlgorithm = raw::CRYPTO_ERRNO_UNSUPPORTED_ALGORITHM.raw(),
16    UnsupportedOption = raw::CRYPTO_ERRNO_UNSUPPORTED_OPTION.raw(),
17    InvalidKey = raw::CRYPTO_ERRNO_INVALID_KEY.raw(),
18    InvalidLength = raw::CRYPTO_ERRNO_INVALID_LENGTH.raw(),
19    VerificationFailed = raw::CRYPTO_ERRNO_VERIFICATION_FAILED.raw(),
20    RngError = raw::CRYPTO_ERRNO_RNG_ERROR.raw(),
21    AlgorithmFailure = raw::CRYPTO_ERRNO_ALGORITHM_FAILURE.raw(),
22    InvalidSignature = raw::CRYPTO_ERRNO_INVALID_SIGNATURE.raw(),
23    Closed = raw::CRYPTO_ERRNO_CLOSED.raw(),
24    InvalidHandle = raw::CRYPTO_ERRNO_INVALID_HANDLE.raw(),
25    Overflow = raw::CRYPTO_ERRNO_OVERFLOW.raw(),
26    InternalError = raw::CRYPTO_ERRNO_INTERNAL_ERROR.raw(),
27    TooManyHandles = raw::CRYPTO_ERRNO_TOO_MANY_HANDLES.raw(),
28    KeyNotSupported = raw::CRYPTO_ERRNO_KEY_NOT_SUPPORTED.raw(),
29    KeyRequired = raw::CRYPTO_ERRNO_KEY_REQUIRED.raw(),
30    InvalidTag = raw::CRYPTO_ERRNO_INVALID_TAG.raw(),
31    InvalidOperation = raw::CRYPTO_ERRNO_INVALID_OPERATION.raw(),
32    NonceRequired = raw::CRYPTO_ERRNO_NONCE_REQUIRED.raw(),
33    InvalidNonce = raw::CRYPTO_ERRNO_INVALID_NONCE.raw(),
34    OptionNotSet = raw::CRYPTO_ERRNO_OPTION_NOT_SET.raw(),
35    NotFound = raw::CRYPTO_ERRNO_NOT_FOUND.raw(),
36    ParametersMissing = raw::CRYPTO_ERRNO_PARAMETERS_MISSING.raw(),
37    InProgress = raw::CRYPTO_ERRNO_IN_PROGRESS.raw(),
38    IncompatibleKeys = raw::CRYPTO_ERRNO_INCOMPATIBLE_KEYS.raw(),
39    Expired = raw::CRYPTO_ERRNO_EXPIRED.raw(),
40}
41
42impl From<raw::CryptoErrno> for Error {
43    fn from(e: raw::CryptoErrno) -> Error {
44        Error::from_raw_error(e.raw()).expect("CryptoErrno should not be success (0)")
45    }
46}
47
48impl Error {
49    pub fn from_raw_error(e: u16) -> Option<Self> {
50        match e {
51            0 => None,
52            e => Some(Error::try_from(e).expect("Unexpected error")),
53        }
54    }
55}