open_dis_rust/
dis_error.rs1use thiserror::Error;
8
9#[derive(Error, Debug)]
10pub enum DISError {
12 #[error("Invalid PDU header: {reason}")]
13 InvalidHeader {
14 reason: String,
15 #[source]
16 source: Option<Box<dyn std::error::Error + Send + Sync>>,
17 },
18
19 #[error("PDU deserialization error: {msg}")]
20 DeserializationError {
21 msg: String,
22 #[source]
23 source: Option<Box<dyn std::error::Error + Send + Sync>>,
24 },
25
26 #[error("PDU serialization error: {msg}")]
27 SerializationError {
28 msg: String,
29 #[source]
30 source: Option<Box<dyn std::error::Error + Send + Sync>>,
31 },
32
33 #[error("Buffer underflow: tried to read {attempted} bytes, but only {available} bytes remain")]
34 BufferUnderflow { attempted: usize, available: usize },
35
36 #[error("PDU size exceeds maximum allowed: {size} > {max_size}")]
37 PduSizeExceeded { size: usize, max_size: usize },
38
39 #[error("PDU wire format error: {0}")]
40 WireFormat(WireFormatError),
41}
42
43#[derive(Debug)]
51pub struct WireFormatError(deku::DekuError);
52
53impl std::fmt::Display for WireFormatError {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 std::fmt::Display::fmt(&self.0, f)
56 }
57}
58
59impl std::error::Error for WireFormatError {}
60
61impl DISError {
62 pub(crate) const fn from_wire(error: deku::DekuError) -> Self {
65 Self::WireFormat(WireFormatError(error))
66 }
67
68 pub fn invalid_header<S: Into<String>>(
70 reason: S,
71 source: Option<Box<dyn std::error::Error + Send + Sync>>,
72 ) -> Self {
73 Self::InvalidHeader {
74 reason: reason.into(),
75 source,
76 }
77 }
78
79 pub fn deserialization_error<S: Into<String>>(
81 msg: S,
82 source: Option<Box<dyn std::error::Error + Send + Sync>>,
83 ) -> Self {
84 Self::DeserializationError {
85 msg: msg.into(),
86 source,
87 }
88 }
89
90 pub fn serialization_error<S: Into<String>>(
92 msg: S,
93 source: Option<Box<dyn std::error::Error + Send + Sync>>,
94 ) -> Self {
95 Self::SerializationError {
96 msg: msg.into(),
97 source,
98 }
99 }
100
101 #[must_use]
102 pub const fn buffer_underflow(attempted: usize, available: usize) -> Self {
104 Self::BufferUnderflow {
105 attempted,
106 available,
107 }
108 }
109
110 #[must_use]
111 pub const fn pdu_size_exceeded(size: usize, max_size: usize) -> Self {
113 Self::PduSizeExceeded { size, max_size }
114 }
115}
116
117pub type DISResult<T> = Result<T, DISError>;