/rust/registry/src/index.crates.io-1949cf8c6b5b557f/ldap-parser-0.5.0/src/error.rs
Line | Count | Source |
1 | | //! LDAP errors |
2 | | |
3 | | use asn1_rs::nom; |
4 | | use asn1_rs::Error; |
5 | | use nom::error::{ErrorKind, FromExternalError, ParseError}; |
6 | | use nom::IResult; |
7 | | |
8 | | /// Holds the result of parsing functions (LDAP) |
9 | | /// |
10 | | /// Note that this type is also a `Result`, so usual functions (`map`, `unwrap` etc.) are available. |
11 | | /// |
12 | | /// Note that this type is not named `LdapResult` to avoid conflicts with LDAP standard type |
13 | | pub type Result<'a, T> = IResult<&'a [u8], T, LdapError>; |
14 | | |
15 | | /// An error that can occur while parsing or validating a certificate. |
16 | | #[derive(Debug, PartialEq, thiserror::Error)] |
17 | | pub enum LdapError { |
18 | | #[error("Invalid LDAP String encoding")] |
19 | | InvalidString, |
20 | | |
21 | | #[error("Invalid LDAP Authentication Type")] |
22 | | InvalidAuthenticationType, |
23 | | |
24 | | #[error("Invalid DN encoding")] |
25 | | InvalidDN, |
26 | | |
27 | | #[error("Invalid Substring Type")] |
28 | | InvalidSubstring, |
29 | | |
30 | | #[error("Invalid Type for Filter")] |
31 | | InvalidFilterType, |
32 | | #[error("Invalid Type for Message")] |
33 | | InvalidMessageType, |
34 | | |
35 | | #[error("Unknown error")] |
36 | | Unknown, |
37 | | |
38 | | #[error("BER error: {0}")] |
39 | | Ber(#[from] Error), |
40 | | #[error("nom error: {0:?}")] |
41 | | NomError(ErrorKind), |
42 | | } |
43 | | |
44 | | impl From<LdapError> for nom::Err<LdapError> { |
45 | 0 | fn from(e: LdapError) -> nom::Err<LdapError> { |
46 | 0 | nom::Err::Error(e) |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl From<ErrorKind> for LdapError { |
51 | 0 | fn from(e: ErrorKind) -> LdapError { |
52 | 0 | LdapError::NomError(e) |
53 | 0 | } |
54 | | } |
55 | | |
56 | | impl<I> ParseError<I> for LdapError { |
57 | 47.3k | fn from_error_kind(_input: I, kind: ErrorKind) -> Self { |
58 | 47.3k | LdapError::NomError(kind) |
59 | 47.3k | } |
60 | 2.40k | fn append(_input: I, kind: ErrorKind, _other: Self) -> Self { |
61 | 2.40k | LdapError::NomError(kind) |
62 | 2.40k | } |
63 | | } |
64 | | |
65 | | impl<I, E> FromExternalError<I, E> for LdapError { |
66 | 0 | fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> LdapError { |
67 | 0 | LdapError::NomError(kind) |
68 | 0 | } |
69 | | } |
70 | | |
71 | | #[allow(dead_code)] |
72 | 0 | pub(crate) fn print_hex_dump(bytes: &[u8], max_len: usize) { |
73 | | use nom::HexDisplay; |
74 | | use std::cmp::min; |
75 | 0 | let m = min(bytes.len(), max_len); |
76 | 0 | if m == 0 { |
77 | 0 | println!("<empty>"); |
78 | 0 | } |
79 | 0 | print!("{}", &bytes[..m].to_hex(16)); |
80 | 0 | if bytes.len() > max_len { |
81 | 0 | println!("... <continued>"); |
82 | 0 | } |
83 | 0 | } |