dxf_tools_rs/
error.rs

1//! Error types for DXF-Tools-RS library
2
3use std::io;
4use thiserror::Error;
5
6/// Main error type for DXF-Tools-RS operations
7#[derive(Debug, Error)]
8pub enum DxfError {
9    /// IO error occurred during file operations
10    #[error("IO error: {0}")]
11    Io(#[from] io::Error),
12
13    /// Unsupported CAD file version
14    #[error("Unsupported CAD version: {0:?}")]
15    UnsupportedVersion(String),
16
17    /// Error during compression/decompression
18    #[error("Compression error: {0}")]
19    Compression(String),
20
21    /// Error parsing CAD file format
22    #[error("Parse error: {0}")]
23    Parse(String),
24
25    /// Invalid DXF code encountered
26    #[error("Invalid DXF code: {0}")]
27    InvalidDxfCode(i32),
28
29    /// Invalid handle reference
30    #[error("Invalid handle: {0:#X}")]
31    InvalidHandle(u64),
32
33    /// Object not found in document
34    #[error("Object not found: handle {0:#X}")]
35    ObjectNotFound(u64),
36
37    /// Invalid entity type
38    #[error("Invalid entity type: {0}")]
39    InvalidEntityType(String),
40
41    /// CRC checksum mismatch
42    #[error("CRC checksum mismatch: expected {expected:#X}, got {actual:#X}")]
43    ChecksumMismatch { expected: u32, actual: u32 },
44
45    /// Invalid file header
46    #[error("Invalid file header: {0}")]
47    InvalidHeader(String),
48
49    /// Invalid file format
50    #[error("Invalid file format: {0}")]
51    InvalidFormat(String),
52
53    /// Invalid sentinel in file
54    #[error("Invalid sentinel: {0}")]
55    InvalidSentinel(String),
56
57    /// Decompression error
58    #[error("Decompression error: {0}")]
59    Decompression(String),
60
61    /// Decryption error
62    #[error("Decryption error: {0}")]
63    Decryption(String),
64
65    /// Encoding error
66    #[error("Encoding error: {0}")]
67    Encoding(String),
68
69    /// Feature not yet implemented
70    #[error("Not implemented: {0}")]
71    NotImplemented(String),
72
73    /// Generic error with custom message
74    #[error("{0}")]
75    Custom(String),
76}
77
78/// Result type alias for DXF-Tools-RS operations
79pub type Result<T> = std::result::Result<T, DxfError>;
80
81impl From<String> for DxfError {
82    fn from(s: String) -> Self {
83        DxfError::Custom(s)
84    }
85}
86
87impl From<&str> for DxfError {
88    fn from(s: &str) -> Self {
89        DxfError::Custom(s.to_string())
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_error_display() {
99        let err = DxfError::UnsupportedVersion("AC1009".to_string());
100        assert_eq!(
101            err.to_string(),
102            "Unsupported CAD version: \"AC1009\""
103        );
104    }
105
106    #[test]
107    fn test_checksum_error() {
108        let err = DxfError::ChecksumMismatch {
109            expected: 0x1234,
110            actual: 0x5678,
111        };
112        assert!(err.to_string().contains("0x1234"));
113        assert!(err.to_string().contains("0x5678"));
114    }
115
116    #[test]
117    fn test_io_error_conversion() {
118        let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
119        let dxf_err: DxfError = io_err.into();
120        assert!(matches!(dxf_err, DxfError::Io(_)));
121    }
122}
123