Skip to main content

buff_rs/
error.rs

1//! Error types for BUFF encoding/decoding operations.
2
3use thiserror::Error;
4
5/// Errors that can occur during BUFF operations.
6#[derive(Debug, Error, Clone, PartialEq)]
7pub enum BuffError {
8    /// The input data is empty.
9    #[error("input data is empty")]
10    EmptyInput,
11
12    /// Invalid precision value (must be >= 0).
13    #[error("invalid precision: {0} (must be >= 0)")]
14    InvalidPrecision(i32),
15
16    /// The encoded data is corrupted or invalid.
17    #[error("invalid encoded data: {0}")]
18    InvalidData(String),
19
20    /// Buffer overflow during bit packing operations.
21    #[error("buffer overflow: attempted to write {attempted} bits, only {available} available")]
22    BufferOverflow {
23        /// The number of bits that were attempted to be written.
24        attempted: usize,
25        /// The number of bits available in the buffer.
26        available: usize,
27    },
28
29    /// The bit width exceeds the maximum supported (32 bits).
30    #[error("bit width {0} exceeds maximum of 32")]
31    BitWidthExceeded(usize),
32
33    /// Special float values (Infinity, NaN) cannot be converted to decimal.
34    #[error("special float value cannot be converted: {0}")]
35    SpecialValueConversion(String),
36
37    /// Precision loss during decimal conversion exceeds acceptable threshold.
38    #[error("precision loss too high: original={original}, converted={converted}")]
39    PrecisionLoss {
40        /// The original value.
41        original: String,
42        /// The converted value.
43        converted: String,
44    },
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_error_display_empty_input() {
53        let err = BuffError::EmptyInput;
54        assert_eq!(err.to_string(), "input data is empty");
55    }
56
57    #[test]
58    fn test_error_display_invalid_precision() {
59        let err = BuffError::InvalidPrecision(-5);
60        assert!(err.to_string().contains("-5"));
61        assert!(err.to_string().contains("must be >= 0"));
62    }
63
64    #[test]
65    fn test_error_display_invalid_data() {
66        let err = BuffError::InvalidData("corrupted header".to_string());
67        assert!(err.to_string().contains("corrupted header"));
68    }
69
70    #[test]
71    fn test_error_display_buffer_overflow() {
72        let err = BuffError::BufferOverflow {
73            attempted: 32,
74            available: 8,
75        };
76        let msg = err.to_string();
77        assert!(msg.contains("32"));
78        assert!(msg.contains("8"));
79    }
80
81    #[test]
82    fn test_error_display_bit_width_exceeded() {
83        let err = BuffError::BitWidthExceeded(64);
84        assert!(err.to_string().contains("64"));
85        assert!(err.to_string().contains("32"));
86    }
87
88    #[test]
89    fn test_error_display_special_value_conversion() {
90        let err = BuffError::SpecialValueConversion("Infinity".to_string());
91        assert!(err.to_string().contains("Infinity"));
92    }
93
94    #[test]
95    fn test_error_display_precision_loss() {
96        let err = BuffError::PrecisionLoss {
97            original: "3.14159".to_string(),
98            converted: "3.14".to_string(),
99        };
100        let msg = err.to_string();
101        assert!(msg.contains("3.14159"));
102        assert!(msg.contains("3.14"));
103    }
104
105    #[test]
106    fn test_error_debug() {
107        let err = BuffError::EmptyInput;
108        let debug_str = format!("{:?}", err);
109        assert!(debug_str.contains("EmptyInput"));
110    }
111
112    #[test]
113    fn test_error_clone() {
114        let err = BuffError::InvalidData("test".to_string());
115        let cloned = err.clone();
116        assert_eq!(err, cloned);
117    }
118
119    #[test]
120    fn test_error_eq() {
121        let err1 = BuffError::EmptyInput;
122        let err2 = BuffError::EmptyInput;
123        assert_eq!(err1, err2);
124
125        let err3 = BuffError::BitWidthExceeded(33);
126        let err4 = BuffError::BitWidthExceeded(33);
127        assert_eq!(err3, err4);
128
129        let err5 = BuffError::BitWidthExceeded(33);
130        let err6 = BuffError::BitWidthExceeded(64);
131        assert_ne!(err5, err6);
132    }
133}