milstd1553b_parser/
error.rs

1//! Error types for MIL-STD-1553B parsing
2
3use thiserror::Error;
4
5/// Result type for MIL-STD-1553B operations
6pub type Result<T> = std::result::Result<T, ParseError>;
7
8/// Error types encountered during MIL-STD-1553B parsing and validation
9#[derive(Error, Debug, Clone, PartialEq, Eq)]
10pub enum ParseError {
11    /// Invalid word format or structure
12    #[error("Invalid word: {0}")]
13    InvalidWord(String),
14
15    /// Parity check failed
16    #[error("Parity error: {0}")]
17    ParityError(String),
18
19    /// Invalid address specified
20    #[error("Invalid address: {0}")]
21    InvalidAddress(String),
22
23    /// Invalid message type
24    #[error("Invalid message type: {0}")]
25    InvalidMessageType(String),
26
27    /// Insufficient data to parse
28    #[error("Insufficient data: {0}")]
29    InsufficientData(String),
30
31    /// Invalid Manchester encoding
32    #[error("Invalid Manchester encoding: {0}")]
33    InvalidManchesterEncoding(String),
34
35    /// Invalid command format
36    #[error("Invalid command: {0}")]
37    InvalidCommand(String),
38
39    /// Invalid response format
40    #[error("Invalid response: {0}")]
41    InvalidResponse(String),
42
43    /// Status word error
44    #[error("Status error: {0}")]
45    StatusError(String),
46
47    /// Bus error detected
48    #[error("Bus error: {0}")]
49    BusError(String),
50
51    /// Generic parsing error
52    #[error("Parse error: {0}")]
53    ParseFailed(String),
54
55    /// Validation error
56    #[error("Validation error: {0}")]
57    ValidationError(String),
58}
59
60impl ParseError {
61    /// Create a new InvalidWord error
62    pub fn invalid_word(msg: impl Into<String>) -> Self {
63        ParseError::InvalidWord(msg.into())
64    }
65
66    /// Create a new ParityError
67    pub fn parity_error(msg: impl Into<String>) -> Self {
68        ParseError::ParityError(msg.into())
69    }
70
71    /// Create a new InvalidAddress error
72    pub fn invalid_address(msg: impl Into<String>) -> Self {
73        ParseError::InvalidAddress(msg.into())
74    }
75
76    /// Create a new InvalidMessageType error
77    pub fn invalid_message_type(msg: impl Into<String>) -> Self {
78        ParseError::InvalidMessageType(msg.into())
79    }
80
81    /// Create a new InsufficientData error
82    pub fn insufficient_data(msg: impl Into<String>) -> Self {
83        ParseError::InsufficientData(msg.into())
84    }
85
86    /// Create a new InvalidManchesterEncoding error
87    pub fn invalid_manchester(msg: impl Into<String>) -> Self {
88        ParseError::InvalidManchesterEncoding(msg.into())
89    }
90
91    /// Create a new ParseFailed error
92    pub fn parse_failed(msg: impl Into<String>) -> Self {
93        ParseError::ParseFailed(msg.into())
94    }
95
96    /// Create a new ValidationError
97    pub fn validation_error(msg: impl Into<String>) -> Self {
98        ParseError::ValidationError(msg.into())
99    }
100
101    /// Create a new InvalidCommand error
102    pub fn invalid_command(msg: impl Into<String>) -> Self {
103        ParseError::InvalidCommand(msg.into())
104    }
105
106    /// Create a new InvalidResponse error
107    pub fn invalid_response(msg: impl Into<String>) -> Self {
108        ParseError::InvalidResponse(msg.into())
109    }
110}
111
112#[cfg(test)]
113mod tests {
114    use super::*;
115
116    #[test]
117    fn test_error_display() {
118        let err = ParseError::invalid_word("test");
119        assert!(err.to_string().contains("Invalid word"));
120    }
121}