blaze_pk/
error.rs

1//! Error type used when decoding packets [`DecodeError`] and result
2//! type alias [`DecodeResult`]
3
4use crate::tag::{Tag, TdfType};
5use std::{error::Error, fmt::Display};
6
7/// Error type for errors that can occur while decoding a value
8/// using the tdf decode
9#[derive(Debug)]
10pub enum DecodeError {
11    /// The tag that was expected could not be found
12    MissingTag {
13        /// The tag that was being searched for
14        tag: Tag,
15        /// The type of the tag being searched for
16        ty: TdfType,
17    },
18    /// The found tag was not of the correct type
19    InvalidTagType {
20        /// The tag which the type was invalid for
21        tag: Tag,
22        /// The expected tdf type
23        expected: TdfType,
24        /// The actual tdf type
25        actual: TdfType,
26    },
27    /// Encountered an unexpected type when decoding a
28    /// map or list
29    InvalidType {
30        /// The expected tdf type
31        expected: TdfType,
32        /// The actual tdf type
33        actual: TdfType,
34    },
35
36    /// Encountered an unknown tag type
37    UnknownType {
38        /// The tag type value
39        ty: u8,
40    },
41
42    /// Reached the end of the available bytes before
43    /// a value could be obtained
44    UnexpectedEof {
45        /// The current reader cusor position
46        cursor: usize,
47        /// The number of bytes attempted to read
48        wanted: usize,
49        /// The remaining bytes in the reader slice
50        remaining: usize,
51    },
52
53    /// Other error type with custom message
54    Other(&'static str),
55}
56
57/// Type alias for result which could result in a Decode Error
58pub type DecodeResult<T> = Result<T, DecodeError>;
59
60/// Error implementation
61impl Error for DecodeError {}
62
63/// Display formatting implementation
64impl Display for DecodeError {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        match self {
67            DecodeError::MissingTag { tag, ty } => {
68                write!(f, "Missing tag '{}' (type: {:?})", tag, ty)
69            }
70            DecodeError::InvalidTagType {
71                tag,
72                expected,
73                actual,
74            } => {
75                write!(
76                    f,
77                    "Invalid tag type for '{}' (expected: {:?}, got: {:?})",
78                    tag, expected, actual
79                )
80            }
81            DecodeError::InvalidType { expected, actual } => {
82                write!(
83                    f,
84                    "Unexpected tag type (expected: {:?}, got: {:?})",
85                    expected, actual
86                )
87            }
88            DecodeError::UnknownType { ty } => {
89                write!(f, "Unknown tag type: {}", ty)
90            }
91            DecodeError::UnexpectedEof {
92                cursor,
93                wanted,
94                remaining,
95            } => {
96                write!(
97                    f,
98                    "Unexpected end of file (cursor: {}, wanted: {}, remaining: {})",
99                    cursor, wanted, remaining
100                )
101            }
102            DecodeError::Other(err) => f.write_str(err),
103        }
104    }
105}