• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)]
panic(self) -> !52     pub(crate) const fn panic(self) -> ! {
53         match self {
54             Error::ArcInvalid { .. } | Error::ArcTooBig => panic!("OID contains invalid arc"),
55             Error::Base128 => panic!("OID contains arc with invalid base 128 encoding"),
56             Error::DigitExpected { .. } => panic!("OID expected to start with digit"),
57             Error::Empty => panic!("OID value is empty"),
58             Error::Length => panic!("OID length invalid"),
59             Error::NotEnoughArcs => panic!("OID requires minimum of 3 arcs"),
60             Error::TrailingDot => panic!("OID ends with invalid trailing '.'"),
61         }
62     }
63 }
64 
65 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result66     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67         match *self {
68             Error::ArcInvalid { arc } => write!(f, "OID contains out-of-range arc: {}", arc),
69             Error::ArcTooBig => f.write_str("OID contains arc which is larger than 32-bits"),
70             Error::Base128 => f.write_str("OID contains arc with invalid base 128 encoding"),
71             Error::DigitExpected { actual } => {
72                 write!(f, "expected digit, got '{}'", char::from(actual))
73             }
74             Error::Empty => f.write_str("OID value is empty"),
75             Error::Length => f.write_str("OID length invalid"),
76             Error::NotEnoughArcs => f.write_str("OID requires minimum of 3 arcs"),
77             Error::TrailingDot => f.write_str("OID ends with invalid trailing '.'"),
78         }
79     }
80 }
81 
82 #[cfg(feature = "std")]
83 impl std::error::Error for Error {}
84