xfcc_parser/
error.rs

1use std::{
2    error::Error,
3    fmt::{Display, Formatter},
4};
5
6use crate::PairKey;
7
8/// XFCC header parsing error
9#[derive(Debug, PartialEq)]
10pub enum XfccError<'a> {
11    /// Used by [`element_list`](crate::element_list) when there is unconsumed data at the
12    /// end of an XFCC header
13    TrailingSequence(&'a [u8]),
14    /// Used by [`element_list`](crate::element_list) when more than one value is given for a
15    /// key that accepts only one value
16    DuplicatePairKey(PairKey),
17    /// Represents an underlying parsing error
18    ParsingError(nom::Err<nom::error::Error<&'a [u8]>>),
19}
20
21impl<'a> Display for XfccError<'a> {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::TrailingSequence(seq) => write!(f, "Trailing sequence {:?}", seq)?,
25            Self::DuplicatePairKey(key) => write!(f, "Duplicate pair key {key}")?,
26            Self::ParsingError(nom_err) => write!(f, "Parsing error {nom_err}")?,
27        }
28        Ok(())
29    }
30}
31
32impl<'a> Error for XfccError<'a> {}
33
34impl<'a> From<nom::Err<nom::error::Error<&'a [u8]>>> for XfccError<'a> {
35    fn from(value: nom::Err<nom::error::Error<&'a [u8]>>) -> Self {
36        Self::ParsingError(value)
37    }
38}