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("Parsing Filter reached maximum depth")]
36 FilterMaxDepth,
37
38 #[error("Unknown error")]
39 Unknown,
40
41 #[error("BER error: {0}")]
42 Ber(#[from] Error),
43 #[error("nom error: {0:?}")]
44 NomError(ErrorKind),
45}
46
47impl From<LdapError> for nom::Err<LdapError> {
48 fn from(e: LdapError) -> nom::Err<LdapError> {
49 nom::Err::Error(e)
50 }
51}
52
53impl From<ErrorKind> for LdapError {
54 fn from(e: ErrorKind) -> LdapError {
55 LdapError::NomError(e)
56 }
57}
58
59impl<I> ParseError<I> for LdapError {
60 fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
61 LdapError::NomError(kind)
62 }
63 fn append(_input: I, kind: ErrorKind, _other: Self) -> Self {
64 LdapError::NomError(kind)
65 }
66}
67
68impl<I, E> FromExternalError<I, E> for LdapError {
69 fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> LdapError {
70 LdapError::NomError(kind)
71 }
72}
73
74#[allow(dead_code)]
75pub(crate) fn print_hex_dump(bytes: &[u8], max_len: usize) {
76 use nom::HexDisplay;
77 use std::cmp::min;
78 let m = min(bytes.len(), max_len);
79 if m == 0 {
80 println!("<empty>");
81 }
82 print!("{}", bytes[..m].to_hex(16));
83 if bytes.len() > max_len {
84 println!("... <continued>");
85 }
86}