sodoken/
error.rs

1/// Error Kind Enum for Sodoken OneErr.
2#[derive(Debug)]
3#[non_exhaustive]
4pub enum SodokenErrKind {
5    /// we were unable to allocate memory
6    AllocationFailed,
7
8    /// the output hash size for this call didn't fall within constraints
9    BadHashSize,
10
11    /// the salt size for this call didn't fall within constraints
12    BadSaltSize,
13
14    /// the key size for this call didn't fall within constraints
15    BadKeySize,
16
17    /// the public key size for this call didn't fall within constraints
18    BadPublicKeySize,
19
20    /// the secret key size for this call didn't fall within constraints
21    BadSecretKeySize,
22
23    /// improper size for signature
24    BadSignatureSize,
25
26    /// improper size for seed
27    BadSeedSize,
28
29    /// improper size for cipher
30    BadCipherSize,
31
32    /// improper size for nonce
33    BadNonceSize,
34
35    /// improper size for message
36    BadMessageSize,
37
38    /// the passphrase size for this call didn't fall within constraints
39    BadPassphraseSize,
40
41    /// the ops limit for this call didn't fall within constraints
42    BadOpsLimit,
43
44    /// the mem limit for this call didn't fall within constraints
45    BadMemLimit,
46
47    /// bad bounds for write operation
48    WriteOverflow,
49
50    /// Internal libsodium error
51    InternalSodium,
52
53    /// Spawn blocking failed, likely due to shutdown
54    SpawnBlocking,
55
56    /// OtherErrorType
57    Other,
58}
59
60impl From<SodokenErrKind> for &'static str {
61    fn from(k: SodokenErrKind) -> Self {
62        use SodokenErrKind::*;
63        match k {
64            AllocationFailed => "AllocationFailed",
65            BadHashSize => "BadHashSize",
66            BadSaltSize => "BadSaltSize",
67            BadKeySize => "BadKeySize",
68            BadPublicKeySize => "BadPublicKeySize",
69            BadSecretKeySize => "BadSecretKeySize",
70            BadSignatureSize => "BadSignatureSize",
71            BadSeedSize => "BadSeedSize",
72            BadCipherSize => "BadCipherSize",
73            BadNonceSize => "BadNonceSize",
74            BadMessageSize => "BadMessageSize",
75            BadPassphraseSize => "BadPassphraseSize",
76            BadOpsLimit => "BadOpsLimit",
77            BadMemLimit => "BadMemLimit",
78            WriteOverflow => "WriteOverflow",
79            InternalSodium => "InternalSodium",
80            SpawnBlocking => "SpawnBlocking",
81            _ => "Other",
82        }
83    }
84}
85
86impl From<&str> for SodokenErrKind {
87    fn from(e: &str) -> Self {
88        use SodokenErrKind::*;
89        match e {
90            "AllocationFailed" => AllocationFailed,
91            "BadHashSize" => BadHashSize,
92            "BadSaltSize" => BadSaltSize,
93            "BadKeySize" => BadKeySize,
94            "BadPublicKeySize" => BadPublicKeySize,
95            "BadSecretKeySize" => BadSecretKeySize,
96            "BadSignatureSize" => BadSignatureSize,
97            "BadSeedSize" => BadSeedSize,
98            "BadCipherSize" => BadCipherSize,
99            "BadNonceSize" => BadNonceSize,
100            "BadMessageSize" => BadMessageSize,
101            "BadPassphraseSize" => BadPassphraseSize,
102            "BadOpsLimit" => BadOpsLimit,
103            "BadMemLimit" => BadMemLimit,
104            "WriteOverflow" => WriteOverflow,
105            "InternalSodium" => InternalSodium,
106            "SpawnBlocking" => SpawnBlocking,
107            _ => Other,
108        }
109    }
110}
111
112impl std::str::FromStr for SodokenErrKind {
113    type Err = one_err::OneErr;
114
115    fn from_str(s: &str) -> Result<Self, Self::Err> {
116        Ok(s.into())
117    }
118}
119
120impl From<&one_err::OneErr> for SodokenErrKind {
121    fn from(e: &one_err::OneErr) -> Self {
122        e.str_kind().into()
123    }
124}
125
126impl From<SodokenErrKind> for one_err::OneErr {
127    fn from(k: SodokenErrKind) -> Self {
128        one_err::OneErr::new(<&'static str>::from(k))
129    }
130}
131
132/// Sodoken Result Type
133pub type SodokenResult<T> = Result<T, one_err::OneErr>;