/rust/registry/src/index.crates.io-1949cf8c6b5b557f/asn1-rs-0.6.2/src/error.rs
Line | Count | Source |
1 | | #![allow(unknown_lints)] |
2 | | #![allow(non_local_definitions)] // false positive for displaydoc::Display: https://github.com/yaahc/displaydoc/issues/46 |
3 | | |
4 | | use crate::{Class, Tag}; |
5 | | use alloc::str; |
6 | | use alloc::string; |
7 | | #[cfg(not(feature = "std"))] |
8 | | use alloc::string::String; |
9 | | use displaydoc::Display; |
10 | | use nom::error::{ErrorKind, FromExternalError, ParseError}; |
11 | | use nom::IResult; |
12 | | #[cfg(feature = "std")] |
13 | | use std::io; |
14 | | #[cfg(feature = "std")] |
15 | | use thiserror::Error; |
16 | | |
17 | | #[cfg(feature = "std")] |
18 | | impl std::error::Error for DerConstraint {} |
19 | | |
20 | | #[derive(Clone, Copy, Debug, Display, PartialEq, Eq)] |
21 | | /// Error types for DER constraints |
22 | | pub enum DerConstraint { |
23 | | /// Indefinite length not allowed |
24 | | IndefiniteLength, |
25 | | /// Object must not be constructed |
26 | | Constructed, |
27 | | /// Object must be constructed |
28 | | NotConstructed, |
29 | | /// DateTime object is missing timezone |
30 | | MissingTimeZone, |
31 | | /// DateTime object is missing seconds |
32 | | MissingSeconds, |
33 | | /// Bitstring unused bits must be set to zero |
34 | | UnusedBitsNotZero, |
35 | | /// Boolean value must be 0x00 of 0xff |
36 | | InvalidBoolean, |
37 | | /// Integer must not be empty |
38 | | IntegerEmpty, |
39 | | /// Leading zeroes in Integer encoding |
40 | | IntegerLeadingZeroes, |
41 | | /// Leading 0xff in negative Integer encoding |
42 | | IntegerLeadingFF, |
43 | | } |
44 | | |
45 | | // XXX |
46 | | // thiserror does not work in no_std |
47 | | // see https://github.com/dtolnay/thiserror/pull/64 |
48 | | |
49 | | #[cfg(feature = "std")] |
50 | | impl std::error::Error for Error {} |
51 | | |
52 | | /// The error type for operations of the [`FromBer`](crate::FromBer), |
53 | | /// [`FromDer`](crate::FromDer), and associated traits. |
54 | | #[derive(Clone, Debug, Display, PartialEq, Eq)] |
55 | | // #[cfg_attr(feature = "std", derive(Error))] |
56 | | pub enum Error { |
57 | | /// BER object does not have the expected type |
58 | | BerTypeError, |
59 | | /// BER object does not have the expected value |
60 | | BerValueError, |
61 | | /// Invalid Length |
62 | | InvalidLength, |
63 | | /// Invalid Value when parsing object with tag {tag:?} {msg:} |
64 | | InvalidValue { tag: Tag, msg: String }, |
65 | | /// Invalid Tag |
66 | | InvalidTag, |
67 | | /// Unknown tag: {0:?} |
68 | | UnknownTag(u32), |
69 | | /// Unexpected Tag (expected: {expected:?}, actual: {actual:?}) |
70 | | UnexpectedTag { expected: Option<Tag>, actual: Tag }, |
71 | | /// Unexpected Class (expected: {expected:?}, actual: {actual:?}) |
72 | | UnexpectedClass { |
73 | | expected: Option<Class>, |
74 | | actual: Class, |
75 | | }, |
76 | | |
77 | | /// Indefinite length not allowed |
78 | | IndefiniteLengthUnexpected, |
79 | | |
80 | | /// DER object was expected to be constructed (and found to be primitive) |
81 | | ConstructExpected, |
82 | | /// DER object was expected to be primitive (and found to be constructed) |
83 | | ConstructUnexpected, |
84 | | |
85 | | /// Integer too large to fit requested type |
86 | | IntegerTooLarge, |
87 | | /// BER integer is negative, while an unsigned integer was requested |
88 | | IntegerNegative, |
89 | | /// BER recursive parsing reached maximum depth |
90 | | BerMaxDepth, |
91 | | |
92 | | /// Invalid encoding or forbidden characters in string |
93 | | StringInvalidCharset, |
94 | | /// Invalid Date or Time |
95 | | InvalidDateTime, |
96 | | |
97 | | /// DER Failed constraint: {0:?} |
98 | | DerConstraintFailed(DerConstraint), |
99 | | |
100 | | /// Requesting borrowed data from a temporary object |
101 | | LifetimeError, |
102 | | /// Feature is not yet implemented |
103 | | Unsupported, |
104 | | |
105 | | /// incomplete data, missing: {0:?} |
106 | | Incomplete(nom::Needed), |
107 | | |
108 | | /// nom error: {0:?} |
109 | | NomError(ErrorKind), |
110 | | } |
111 | | |
112 | | impl Error { |
113 | | /// Build an error from the provided invalid value |
114 | | #[inline] |
115 | 1.09k | pub const fn invalid_value(tag: Tag, msg: String) -> Self { |
116 | 1.09k | Self::InvalidValue { tag, msg } |
117 | 1.09k | } |
118 | | |
119 | | /// Build an error from the provided unexpected class |
120 | | #[inline] |
121 | 2.09k | pub const fn unexpected_class(expected: Option<Class>, actual: Class) -> Self { |
122 | 2.09k | Self::UnexpectedClass { expected, actual } |
123 | 2.09k | } |
124 | | |
125 | | /// Build an error from the provided unexpected tag |
126 | | #[inline] |
127 | 899 | pub const fn unexpected_tag(expected: Option<Tag>, actual: Tag) -> Self { |
128 | 899 | Self::UnexpectedTag { expected, actual } |
129 | 899 | } |
130 | | } |
131 | | |
132 | | impl<'a> ParseError<&'a [u8]> for Error { |
133 | 59.5k | fn from_error_kind(_input: &'a [u8], kind: ErrorKind) -> Self { |
134 | 59.5k | Error::NomError(kind) |
135 | 59.5k | } |
136 | 140 | fn append(_input: &'a [u8], kind: ErrorKind, _other: Self) -> Self { |
137 | 140 | Error::NomError(kind) |
138 | 140 | } |
139 | | } |
140 | | |
141 | | impl From<Error> for nom::Err<Error> { |
142 | 270k | fn from(e: Error) -> Self { |
143 | 270k | nom::Err::Error(e) |
144 | 270k | } |
145 | | } |
146 | | |
147 | | impl From<str::Utf8Error> for Error { |
148 | 2.89k | fn from(_: str::Utf8Error) -> Self { |
149 | 2.89k | Error::StringInvalidCharset |
150 | 2.89k | } |
151 | | } |
152 | | |
153 | | impl From<string::FromUtf8Error> for Error { |
154 | 0 | fn from(_: string::FromUtf8Error) -> Self { |
155 | 0 | Error::StringInvalidCharset |
156 | 0 | } |
157 | | } |
158 | | |
159 | | impl From<string::FromUtf16Error> for Error { |
160 | 0 | fn from(_: string::FromUtf16Error) -> Self { |
161 | 0 | Error::StringInvalidCharset |
162 | 0 | } |
163 | | } |
164 | | |
165 | | impl From<nom::Err<Error>> for Error { |
166 | 21.1k | fn from(e: nom::Err<Error>) -> Self { |
167 | 21.1k | match e { |
168 | 3.36k | nom::Err::Incomplete(n) => Self::Incomplete(n), |
169 | 17.7k | nom::Err::Error(e) | nom::Err::Failure(e) => e, |
170 | | } |
171 | 21.1k | } |
172 | | } |
173 | | |
174 | | impl<I, E> FromExternalError<I, E> for Error { |
175 | 12.9k | fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> Error { |
176 | 12.9k | Error::NomError(kind) |
177 | 12.9k | } <asn1_rs::error::Error as nom::error::FromExternalError<&[u8], snmp_parser::error::SnmpError>>::from_external_error Line | Count | Source | 175 | 12.9k | fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> Error { | 176 | 12.9k | Error::NomError(kind) | 177 | 12.9k | } |
Unexecuted instantiation: <asn1_rs::error::Error as nom::error::FromExternalError<&[u8], asn1_rs::error::Error>>::from_external_error Unexecuted instantiation: <asn1_rs::error::Error as nom::error::FromExternalError<_, _>>::from_external_error |
178 | | } |
179 | | |
180 | | /// Flatten all `nom::Err` variants error into a single error type |
181 | 0 | pub fn from_nom_error<E, F>(e: nom::Err<E>) -> F |
182 | 0 | where |
183 | 0 | F: From<E> + From<Error>, |
184 | | { |
185 | 0 | match e { |
186 | 0 | nom::Err::Error(e) | nom::Err::Failure(e) => F::from(e), |
187 | 0 | nom::Err::Incomplete(n) => F::from(Error::Incomplete(n)), |
188 | | } |
189 | 0 | } Unexecuted instantiation: asn1_rs::error::from_nom_error::<asn1_rs::error::Error, x509_parser::error::X509Error> Unexecuted instantiation: asn1_rs::error::from_nom_error::<x509_parser::error::X509Error, x509_parser::error::X509Error> Unexecuted instantiation: asn1_rs::error::from_nom_error::<_, _> |
190 | | |
191 | | /// Holds the result of BER/DER serialization functions |
192 | | pub type ParseResult<'a, T, E = Error> = IResult<&'a [u8], T, E>; |
193 | | |
194 | | /// A specialized `Result` type for all operations from this crate. |
195 | | pub type Result<T, E = Error> = core::result::Result<T, E>; |
196 | | |
197 | | /// The error type for serialization operations of the [`ToDer`](crate::ToDer) trait. |
198 | | #[cfg(feature = "std")] |
199 | | #[derive(Debug, Error)] |
200 | | pub enum SerializeError { |
201 | | #[error("ASN.1 error: {0:?}")] |
202 | | ASN1Error(#[from] Error), |
203 | | |
204 | | #[error("Invalid Class {class:}")] |
205 | | InvalidClass { class: u8 }, |
206 | | |
207 | | #[error("Invalid Length")] |
208 | | InvalidLength, |
209 | | |
210 | | #[error("I/O error: {0:?}")] |
211 | | IOError(#[from] io::Error), |
212 | | } |
213 | | |
214 | | #[cfg(feature = "std")] |
215 | | /// Holds the result of BER/DER encoding functions |
216 | | pub type SerializeResult<T> = std::result::Result<T, SerializeError>; |