Skip to main content

open_dis_rust/
dis_error.rs

1//     open-dis-rust - Rust implementation of the IEEE 1278.1-2012 Distributed Interactive
2//                     Simulation (DIS) application protocol
3//     Copyright (C) 2026 Cameron Howell
4//
5//     Licensed under the BSD 2-Clause License
6
7use thiserror::Error;
8
9#[derive(Error, Debug)]
10/// Enhanced error types for DIS protocol operations
11pub 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/// The bytes do not form a valid PDU. This is usually caused by the buffer being
44/// truncated partway through a field, an enumerated field held an undefined value,
45/// or a record's fields were inconsistent.
46///
47/// The message describes what went wrong. The error from the crate's internal
48/// encoding library is deliberately not exposed so that the library can change
49/// without breaking this crate's API.
50#[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    /// Wraps an error from the encoding library. Internal: keeps `deku` out of the
63    /// public API, which a `From` impl would set.
64    pub(crate) const fn from_wire(error: deku::DekuError) -> Self {
65        Self::WireFormat(WireFormatError(error))
66    }
67
68    /// Create a new `InvalidHeader` error with optional source error
69    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    /// Create a new `DeserializationError` with optional source error
80    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    /// Create a new `SerializationError` with optional source error
91    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    /// Create a new `BufferUnderflow` error
103    pub const fn buffer_underflow(attempted: usize, available: usize) -> Self {
104        Self::BufferUnderflow {
105            attempted,
106            available,
107        }
108    }
109
110    #[must_use]
111    /// Create a new `PduSizeExceeded` error
112    pub const fn pdu_size_exceeded(size: usize, max_size: usize) -> Self {
113        Self::PduSizeExceeded { size, max_size }
114    }
115}
116
117/// Result type for DIS operations
118pub type DISResult<T> = Result<T, DISError>;