netbios_parser/
error.rs

1use crate::nom;
2use nom::{
3    error::{ErrorKind, ParseError},
4    IResult,
5};
6
7pub type Result<'a, T> = IResult<&'a [u8], T, NetbiosError<&'a [u8]>>;
8
9/// An error that can occur while parsing or validating a NetBIOS packet.
10#[derive(Debug, PartialEq, thiserror::Error)]
11pub enum NetbiosError<I>
12where
13    I: std::fmt::Debug,
14{
15    #[error("generic error")]
16    Generic,
17
18    #[error("invalid name length")]
19    InvalidNameLength,
20
21    #[error("invalid NetBIOS name in Name Service question field")]
22    InvalidQuestion,
23
24    #[error("invalid NetBIOS name in Name Service answer field")]
25    InvalidAnswer,
26
27    #[error("nom error: {0:?}")]
28    NomError(I, ErrorKind),
29}
30
31impl<I> From<NetbiosError<I>> for nom::Err<NetbiosError<I>>
32where
33    I: std::fmt::Debug,
34{
35    fn from(e: NetbiosError<I>) -> nom::Err<NetbiosError<I>> {
36        nom::Err::Error(e)
37    }
38}
39
40impl<I> ParseError<I> for NetbiosError<I>
41where
42    I: std::fmt::Debug,
43{
44    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
45        NetbiosError::NomError(input, kind)
46    }
47    fn append(input: I, kind: ErrorKind, _other: Self) -> Self {
48        NetbiosError::NomError(input, kind)
49    }
50}