1use asn1_rs::nom;
4use asn1_rs::Error;
5use nom::error::{ErrorKind, FromExternalError, ParseError};
6use nom::IResult;
7
8pub type Result<'a, T> = IResult<&'a [u8], T, LdapError>;
14
15#[derive(Debug, PartialEq, thiserror::Error)]
17pub 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
44impl From<LdapError> for nom::Err<LdapError> {
45 fn from(e: LdapError) -> nom::Err<LdapError> {
46 nom::Err::Error(e)
47 }
48}
49
50impl From<ErrorKind> for LdapError {
51 fn from(e: ErrorKind) -> LdapError {
52 LdapError::NomError(e)
53 }
54}
55
56impl<I> ParseError<I> for LdapError {
57 fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
58 LdapError::NomError(kind)
59 }
60 fn append(_input: I, kind: ErrorKind, _other: Self) -> Self {
61 LdapError::NomError(kind)
62 }
63}
64
65impl<I, E> FromExternalError<I, E> for LdapError {
66 fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> LdapError {
67 LdapError::NomError(kind)
68 }
69}
70
71#[allow(dead_code)]
72pub(crate) fn print_hex_dump(bytes: &[u8], max_len: usize) {
73 use nom::HexDisplay;
74 use std::cmp::min;
75 let m = min(bytes.len(), max_len);
76 if m == 0 {
77 println!("<empty>");
78 }
79 print!("{}", &bytes[..m].to_hex(16));
80 if bytes.len() > max_len {
81 println!("... <continued>");
82 }
83}