const_oid/
error.rs

1//! Error types
2
3use crate::Arc;
4use core::fmt;
5
6/// Result type
7pub type Result<T> = core::result::Result<T, Error>;
8
9/// OID errors.
10#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
11pub 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
52impl 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    pub(crate) const fn panic(self) -> ! {
58        match self {
59            Error::ArcInvalid { .. } | Error::ArcTooBig => panic!("OID contains invalid arc"),
60            Error::Base128 => panic!("OID contains arc with invalid base 128 encoding"),
61            Error::DigitExpected { .. } => panic!("OID expected to start with digit"),
62            Error::Empty => panic!("OID value is empty"),
63            Error::Length => panic!("OID length invalid"),
64            Error::Overflow => panic!("arithmetic calculation overflowed"),
65            Error::RepeatedDot => panic!("repeated consecutive '..' characters in OID"),
66            Error::TrailingDot => panic!("OID ends with invalid trailing '.'"),
67        }
68    }
69}
70
71impl fmt::Display for Error {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match *self {
74            Error::ArcInvalid { arc } => write!(f, "OID contains out-of-range arc: {}", arc),
75            Error::ArcTooBig => f.write_str("OID contains arc which is larger than 32-bits"),
76            Error::Base128 => f.write_str("OID contains arc with invalid base 128 encoding"),
77            Error::DigitExpected { actual } => {
78                write!(f, "expected digit, got '{}'", char::from(actual))
79            }
80            Error::Empty => f.write_str("OID value is empty"),
81            Error::Length => f.write_str("OID length invalid"),
82            Error::Overflow => f.write_str("arithmetic calculation overflowed"),
83            Error::RepeatedDot => f.write_str("repeated consecutive '..' characters in OID"),
84            Error::TrailingDot => f.write_str("OID ends with invalid trailing '.'"),
85        }
86    }
87}
88
89impl core::error::Error for Error {}