Skip to main content

flowparser_sflow/
error.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Errors that can occur when parsing sFlow datagrams.
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub enum SflowError {
7    /// The input buffer is too short to read the expected field.
8    Incomplete {
9        /// Number of bytes available.
10        available: usize,
11        /// Description of the field being parsed.
12        context: String,
13    },
14    /// The datagram version is not sFlow v5.
15    UnsupportedVersion {
16        /// The version number found in the datagram header.
17        version: u32,
18    },
19    /// A structural parse error at a known offset.
20    ParseError {
21        /// Byte offset from the start of the datagram.
22        offset: usize,
23        /// Description of what was being parsed.
24        context: String,
25        /// Description of the error.
26        kind: String,
27    },
28    /// The datagram contains more samples than the configured limit.
29    TooManySamples {
30        /// Number of samples declared in the datagram header.
31        count: u32,
32        /// Configured maximum.
33        max: u32,
34    },
35}
36
37impl fmt::Display for SflowError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            SflowError::Incomplete { available, context } => {
41                write!(
42                    f,
43                    "Incomplete data: only {available} bytes available ({context})"
44                )
45            }
46            SflowError::UnsupportedVersion { version } => {
47                write!(f, "Unsupported sFlow version: {version} (expected 5)")
48            }
49            SflowError::ParseError {
50                offset,
51                context,
52                kind,
53            } => {
54                write!(f, "Parse error at offset {offset}: {kind} ({context})")
55            }
56            SflowError::TooManySamples { count, max } => {
57                write!(f, "Too many samples: {count} exceeds maximum of {max}")
58            }
59        }
60    }
61}
62
63impl std::error::Error for SflowError {}