bgpkit_parser/
error.rs

1/*!
2error module defines the error types used in bgpkit-parser.
3*/
4use crate::models::{Afi, Bgp4MpType, BgpState, EntryType, Safi, TableDumpV2Type};
5use num_enum::TryFromPrimitiveError;
6#[cfg(feature = "oneio")]
7use oneio::OneIoError;
8use std::fmt::{Display, Formatter};
9use std::io::ErrorKind;
10use std::{error::Error, fmt, io};
11
12#[derive(Debug)]
13pub enum ParserError {
14    IoError(io::Error),
15    EofError(io::Error),
16    #[cfg(feature = "oneio")]
17    OneIoError(OneIoError),
18    EofExpected,
19    ParseError(String),
20    TruncatedMsg(String),
21    Unsupported(String),
22    FilterError(String),
23}
24
25impl Error for ParserError {}
26
27#[derive(Debug)]
28pub struct ParserErrorWithBytes {
29    pub error: ParserError,
30    pub bytes: Option<Vec<u8>>,
31}
32
33impl Display for ParserErrorWithBytes {
34    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.error)
36    }
37}
38
39impl Error for ParserErrorWithBytes {}
40
41/// implement Display trait for Error which satistifies the std::error::Error
42/// trait's requirement (must implement Display and Debug traits, Debug already derived)
43impl Display for ParserError {
44    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45        match self {
46            ParserError::IoError(e) => write!(f, "Error: {}", e),
47            ParserError::EofError(e) => write!(f, "Error: {}", e),
48            ParserError::ParseError(s) => write!(f, "Error: {}", s),
49            ParserError::TruncatedMsg(s) => write!(f, "Error: {}", s),
50            ParserError::Unsupported(s) => write!(f, "Error: {}", s),
51            ParserError::EofExpected => write!(f, "Error: reach end of file"),
52            #[cfg(feature = "oneio")]
53            ParserError::OneIoError(e) => write!(f, "Error: {}", e),
54            ParserError::FilterError(e) => write!(f, "Error: {}", e),
55        }
56    }
57}
58
59#[cfg(feature = "oneio")]
60impl From<OneIoError> for ParserErrorWithBytes {
61    fn from(error: OneIoError) -> Self {
62        ParserErrorWithBytes {
63            error: ParserError::OneIoError(error),
64            bytes: None,
65        }
66    }
67}
68
69#[cfg(feature = "oneio")]
70impl From<OneIoError> for ParserError {
71    fn from(error: OneIoError) -> Self {
72        ParserError::OneIoError(error)
73    }
74}
75
76impl From<ParserError> for ParserErrorWithBytes {
77    fn from(error: ParserError) -> Self {
78        ParserErrorWithBytes { error, bytes: None }
79    }
80}
81
82impl From<io::Error> for ParserError {
83    fn from(io_error: io::Error) -> Self {
84        match io_error.kind() {
85            ErrorKind::UnexpectedEof => ParserError::EofError(io_error),
86            _ => ParserError::IoError(io_error),
87        }
88    }
89}
90
91impl From<TryFromPrimitiveError<Bgp4MpType>> for ParserError {
92    fn from(value: TryFromPrimitiveError<Bgp4MpType>) -> Self {
93        ParserError::ParseError(format!("cannot parse bgp4mp subtype: {}", value.number))
94    }
95}
96
97impl From<TryFromPrimitiveError<BgpState>> for ParserError {
98    fn from(value: TryFromPrimitiveError<BgpState>) -> Self {
99        ParserError::ParseError(format!("cannot parse bgp4mp state: {}", value.number))
100    }
101}
102
103impl From<TryFromPrimitiveError<TableDumpV2Type>> for ParserError {
104    fn from(value: TryFromPrimitiveError<TableDumpV2Type>) -> Self {
105        ParserError::ParseError(format!("cannot parse table dump v2 type: {}", value.number))
106    }
107}
108
109impl From<TryFromPrimitiveError<EntryType>> for ParserError {
110    fn from(value: TryFromPrimitiveError<EntryType>) -> Self {
111        ParserError::ParseError(format!("cannot parse entry type: {}", value.number))
112    }
113}
114
115impl From<TryFromPrimitiveError<Afi>> for ParserError {
116    fn from(value: TryFromPrimitiveError<Afi>) -> Self {
117        ParserError::ParseError(format!("Unknown AFI type: {}", value.number))
118    }
119}
120
121impl From<TryFromPrimitiveError<Safi>> for ParserError {
122    fn from(value: TryFromPrimitiveError<Safi>) -> Self {
123        ParserError::ParseError(format!("Unknown SAFI type: {}", value.number))
124    }
125}