orion/
errors.rs

1// MIT License
2
3// Copyright (c) 2018-2025 The orion Developers
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23use core::fmt;
24
25#[allow(clippy::derive_partial_eq_without_eq)]
26/// Opaque error.
27#[derive(Clone, Copy, PartialEq)]
28pub struct UnknownCryptoError;
29
30impl fmt::Display for UnknownCryptoError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "UnknownCryptoError")
33    }
34}
35
36impl fmt::Debug for UnknownCryptoError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(f, "UnknownCryptoError")
39    }
40}
41
42impl core::error::Error for UnknownCryptoError {
43    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
44        None
45    }
46}
47
48#[cfg(feature = "safe_api")]
49impl From<getrandom::Error> for UnknownCryptoError {
50    fn from(_: getrandom::Error) -> Self {
51        UnknownCryptoError
52    }
53}
54
55#[cfg(feature = "safe_api")]
56impl From<ct_codecs::Error> for UnknownCryptoError {
57    fn from(_: ct_codecs::Error) -> Self {
58        UnknownCryptoError
59    }
60}
61
62impl From<core::num::ParseIntError> for UnknownCryptoError {
63    fn from(_: core::num::ParseIntError) -> Self {
64        UnknownCryptoError
65    }
66}
67
68#[test]
69#[cfg(feature = "safe_api")]
70// format! is only available with std
71fn test_unknown_crypto_error_debug_display() {
72    // Tests Debug impl through "{:?}"
73    let err = format!("{:?}", UnknownCryptoError);
74    assert_eq!(err, "UnknownCryptoError");
75    // Tests Display impl through "{}"
76    let err = format!("{}", UnknownCryptoError);
77    assert_eq!(err, "UnknownCryptoError");
78}
79
80#[test]
81#[cfg(feature = "safe_api")]
82// format! is only available with std
83fn test_unknown_crypto_from_getrandom() {
84    // Choose some random error code.
85    let err_code: u16 = 12;
86    let err_foreign: getrandom::Error = getrandom::Error::new_custom(err_code);
87
88    // Tests Debug impl through "{:?}"
89    let err = format!("{:?}", UnknownCryptoError::from(err_foreign));
90    assert_eq!(err, "UnknownCryptoError");
91    // Tests Display impl through "{}"
92    let err = format!("{}", UnknownCryptoError::from(err_foreign));
93    assert_eq!(err, "UnknownCryptoError");
94}
95
96#[test]
97fn test_source() {
98    use core::error::Error;
99    assert!(UnknownCryptoError.source().is_none());
100}
101
102#[test]
103#[cfg(feature = "safe_api")]
104fn test_unknown_crypto_from_decode_error() {
105    use ct_codecs::Error;
106
107    let err_one = Error::InvalidInput;
108    let err_two = Error::Overflow;
109
110    // Tests Debug impl through "{:?}" and Display impl though "{}"
111    let err = format!(
112        "{:?}:{}",
113        UnknownCryptoError::from(err_one),
114        UnknownCryptoError::from(err_one)
115    );
116    assert_eq!(err, "UnknownCryptoError:UnknownCryptoError");
117    let err = format!(
118        "{:?}:{}",
119        UnknownCryptoError::from(err_two),
120        UnknownCryptoError::from(err_two)
121    );
122    assert_eq!(err, "UnknownCryptoError:UnknownCryptoError");
123}
124
125#[test]
126#[cfg(feature = "safe_api")]
127fn test_unknown_crypto_from_parseint_error() {
128    let err_foreign = "j".parse::<u32>().unwrap_err();
129
130    // Tests Debug impl through "{:?}" and Display impl though "{}"
131    let err = format!(
132        "{:?}:{}",
133        UnknownCryptoError::from(err_foreign.clone()),
134        UnknownCryptoError::from(err_foreign)
135    );
136    assert_eq!(err, "UnknownCryptoError:UnknownCryptoError");
137}