/rust/registry/src/index.crates.io-1949cf8c6b5b557f/const-oid-0.10.2/src/error.rs
Line | Count | Source |
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 | | /// Arithmetic overflow (or underflow) errors. |
41 | | /// |
42 | | /// These generally indicate a bug in the `const-oid` crate. |
43 | | Overflow, |
44 | | |
45 | | /// Repeated `..` characters in input data. |
46 | | RepeatedDot, |
47 | | |
48 | | /// Trailing `.` character at end of input. |
49 | | TrailingDot, |
50 | | } |
51 | | |
52 | | impl Error { |
53 | | /// Escalate this error into a panic. |
54 | | /// |
55 | | /// This is a workaround until `Result::unwrap` is allowed in `const fn`. |
56 | | #[allow(clippy::panic)] |
57 | 0 | pub(crate) const fn panic(self) -> ! { |
58 | 0 | match self { |
59 | 0 | Error::ArcInvalid { .. } | Error::ArcTooBig => panic!("OID contains invalid arc"), |
60 | 0 | Error::Base128 => panic!("OID contains arc with invalid base 128 encoding"), |
61 | 0 | Error::DigitExpected { .. } => panic!("OID expected to start with digit"), |
62 | 0 | Error::Empty => panic!("OID value is empty"), |
63 | 0 | Error::Length => panic!("OID length invalid"), |
64 | 0 | Error::Overflow => panic!("arithmetic calculation overflowed"), |
65 | 0 | Error::RepeatedDot => panic!("repeated consecutive '..' characters in OID"), |
66 | 0 | Error::TrailingDot => panic!("OID ends with invalid trailing '.'"), |
67 | | } |
68 | | } |
69 | | } |
70 | | |
71 | | impl fmt::Display for Error { |
72 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
73 | 0 | match *self { |
74 | 0 | Error::ArcInvalid { arc } => write!(f, "OID contains out-of-range arc: {arc}"), |
75 | 0 | Error::ArcTooBig => f.write_str("OID contains arc which is larger than 32-bits"), |
76 | 0 | Error::Base128 => f.write_str("OID contains arc with invalid base 128 encoding"), |
77 | 0 | Error::DigitExpected { actual } => { |
78 | 0 | write!(f, "expected digit, got '{}'", char::from(actual)) |
79 | | } |
80 | 0 | Error::Empty => f.write_str("OID value is empty"), |
81 | 0 | Error::Length => f.write_str("OID length invalid"), |
82 | 0 | Error::Overflow => f.write_str("arithmetic calculation overflowed"), |
83 | 0 | Error::RepeatedDot => f.write_str("repeated consecutive '..' characters in OID"), |
84 | 0 | Error::TrailingDot => f.write_str("OID ends with invalid trailing '.'"), |
85 | | } |
86 | 0 | } |
87 | | } |
88 | | |
89 | | impl core::error::Error for Error {} |