Skip to main content

ipfix_parser/parsers/
error.rs

1//! Parser error types
2
3use std::fmt;
4
5/// Parser error type
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum ParseError {
8    /// The input ended before a structure could be fully read
9    Truncated {
10        /// Structure being read
11        context: &'static str,
12        /// Octets needed
13        needed: usize,
14        /// Octets available
15        available: usize,
16    },
17    /// Version Number is not 10 (IPFIX)
18    InvalidVersion(u16),
19    /// Invalid data format
20    InvalidData(String),
21    /// The decoder Template cache is full (see
22    /// [`DecoderConfig::max_templates`](crate::DecoderConfig::max_templates))
23    TemplateLimitExceeded(usize),
24}
25
26impl fmt::Display for ParseError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            ParseError::Truncated {
30                context,
31                needed,
32                available,
33            } => write!(
34                f,
35                "Truncated {}: needed {} octets, {} available",
36                context, needed, available
37            ),
38            ParseError::InvalidVersion(v) => {
39                write!(f, "Invalid version: {} (expected 10)", v)
40            }
41            ParseError::InvalidData(msg) => write!(f, "Invalid data: {}", msg),
42            ParseError::TemplateLimitExceeded(limit) => {
43                write!(f, "Template limit exceeded: {} templates", limit)
44            }
45        }
46    }
47}
48
49impl std::error::Error for ParseError {}
50
51/// Result type for parser operations
52pub type Result<T> = std::result::Result<T, ParseError>;