1use std::io;
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum DxfError {
9 #[error("IO error: {0}")]
11 Io(#[from] io::Error),
12
13 #[error("Unsupported CAD version: {0:?}")]
15 UnsupportedVersion(String),
16
17 #[error("Compression error: {0}")]
19 Compression(String),
20
21 #[error("Parse error: {0}")]
23 Parse(String),
24
25 #[error("Invalid DXF code: {0}")]
27 InvalidDxfCode(i32),
28
29 #[error("Invalid handle: {0:#X}")]
31 InvalidHandle(u64),
32
33 #[error("Object not found: handle {0:#X}")]
35 ObjectNotFound(u64),
36
37 #[error("Invalid entity type: {0}")]
39 InvalidEntityType(String),
40
41 #[error("CRC checksum mismatch: expected {expected:#X}, got {actual:#X}")]
43 ChecksumMismatch { expected: u32, actual: u32 },
44
45 #[error("Invalid file header: {0}")]
47 InvalidHeader(String),
48
49 #[error("Invalid file format: {0}")]
51 InvalidFormat(String),
52
53 #[error("Invalid sentinel: {0}")]
55 InvalidSentinel(String),
56
57 #[error("Decompression error: {0}")]
59 Decompression(String),
60
61 #[error("Decryption error: {0}")]
63 Decryption(String),
64
65 #[error("Encoding error: {0}")]
67 Encoding(String),
68
69 #[error("Not implemented: {0}")]
71 NotImplemented(String),
72
73 #[error("{0}")]
75 Custom(String),
76}
77
78pub 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