/rust/registry/src/index.crates.io-6f17d22bba15001f/const-oid-0.9.6/src/error.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Error types |
2 | | |
3 | | use crate::Arc; |
4 | | use core::fmt; |
5 | | |
6 | | /// Result type |
7 | | pub type Result<T> = core::result::Result<T, Error>; |
8 | | |
9 | | /// OID errors. |
10 | | #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] |
11 | | pub enum Error { |
12 | | /// Arc exceeds allowed range (i.e. for first or second OID) |
13 | | ArcInvalid { |
14 | | /// Arc value that is erroneous. |
15 | | arc: Arc, |
16 | | }, |
17 | | |
18 | | /// Arc is too big (exceeds 32-bit limits of this library). |
19 | | /// |
20 | | /// Technically the size of an arc is not constrained by X.660, however |
21 | | /// this library has elected to use `u32` as the arc representation as |
22 | | /// sufficient for PKIX/PKCS usages. |
23 | | ArcTooBig, |
24 | | |
25 | | /// Base 128 encoding error (used in BER/DER serialization of arcs). |
26 | | Base128, |
27 | | |
28 | | /// Expected a digit, but was provided something else. |
29 | | DigitExpected { |
30 | | /// What was found instead of a digit |
31 | | actual: u8, |
32 | | }, |
33 | | |
34 | | /// Input data is empty. |
35 | | Empty, |
36 | | |
37 | | /// OID length is invalid (too short or too long). |
38 | | Length, |
39 | | |
40 | | /// Minimum 3 arcs required. |
41 | | NotEnoughArcs, |
42 | | |
43 | | /// Trailing `.` character at end of input. |
44 | | TrailingDot, |
45 | | } |
46 | | |
47 | | impl Error { |
48 | | /// Escalate this error into a panic. |
49 | | /// |
50 | | /// This is a workaround until `Result::unwrap` is allowed in `const fn`. |
51 | | #[allow(clippy::panic)] |
52 | 0 | pub(crate) const fn panic(self) -> ! { |
53 | 0 | match self { |
54 | 0 | Error::ArcInvalid { .. } | Error::ArcTooBig => panic!("OID contains invalid arc"), |
55 | 0 | Error::Base128 => panic!("OID contains arc with invalid base 128 encoding"), |
56 | 0 | Error::DigitExpected { .. } => panic!("OID expected to start with digit"), |
57 | 0 | Error::Empty => panic!("OID value is empty"), |
58 | 0 | Error::Length => panic!("OID length invalid"), |
59 | 0 | Error::NotEnoughArcs => panic!("OID requires minimum of 3 arcs"), |
60 | 0 | Error::TrailingDot => panic!("OID ends with invalid trailing '.'"), |
61 | | } |
62 | | } |
63 | | } |
64 | | |
65 | | impl fmt::Display for Error { |
66 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
67 | 0 | match *self { |
68 | 0 | Error::ArcInvalid { arc } => write!(f, "OID contains out-of-range arc: {}", arc), |
69 | 0 | Error::ArcTooBig => f.write_str("OID contains arc which is larger than 32-bits"), |
70 | 0 | Error::Base128 => f.write_str("OID contains arc with invalid base 128 encoding"), |
71 | 0 | Error::DigitExpected { actual } => { |
72 | 0 | write!(f, "expected digit, got '{}'", char::from(actual)) |
73 | | } |
74 | 0 | Error::Empty => f.write_str("OID value is empty"), |
75 | 0 | Error::Length => f.write_str("OID length invalid"), |
76 | 0 | Error::NotEnoughArcs => f.write_str("OID requires minimum of 3 arcs"), |
77 | 0 | Error::TrailingDot => f.write_str("OID ends with invalid trailing '.'"), |
78 | | } |
79 | 0 | } |
80 | | } |
81 | | |
82 | | #[cfg(feature = "std")] |
83 | | impl std::error::Error for Error {} |