use std::io;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum DxfError {
#[error("IO error: {0}")]
Io(#[from] io::Error),
#[error("Unsupported CAD version: {0:?}")]
UnsupportedVersion(String),
#[error("Compression error: {0}")]
Compression(String),
#[error("Parse error: {0}")]
Parse(String),
#[error("Invalid DXF code: {0}")]
InvalidDxfCode(i32),
#[error("Invalid handle: {0:#X}")]
InvalidHandle(u64),
#[error("Object not found: handle {0:#X}")]
ObjectNotFound(u64),
#[error("Invalid entity type: {0}")]
InvalidEntityType(String),
#[error("CRC checksum mismatch: expected {expected:#X}, got {actual:#X}")]
ChecksumMismatch { expected: u32, actual: u32 },
#[error("Invalid file header: {0}")]
InvalidHeader(String),
#[error("Invalid file format: {0}")]
InvalidFormat(String),
#[error("Invalid sentinel: {0}")]
InvalidSentinel(String),
#[error("Decompression error: {0}")]
Decompression(String),
#[error("Decryption error: {0}")]
Decryption(String),
#[error("Encoding error: {0}")]
Encoding(String),
#[error("Not implemented: {0}")]
NotImplemented(String),
#[cfg(feature = "import")]
#[error("Import error: {0}")]
ImportError(String),
#[error("{0}")]
Custom(String),
}
pub type Result<T> = std::result::Result<T, DxfError>;
impl From<String> for DxfError {
fn from(s: String) -> Self {
DxfError::Custom(s)
}
}
impl From<&str> for DxfError {
fn from(s: &str) -> Self {
DxfError::Custom(s.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = DxfError::UnsupportedVersion("AC1009".to_string());
assert_eq!(
err.to_string(),
"Unsupported CAD version: \"AC1009\""
);
}
#[test]
fn test_checksum_error() {
let err = DxfError::ChecksumMismatch {
expected: 0x1234,
actual: 0x5678,
};
assert!(err.to_string().contains("0x1234"));
assert!(err.to_string().contains("0x5678"));
}
#[test]
fn test_io_error_conversion() {
let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let dxf_err: DxfError = io_err.into();
assert!(matches!(dxf_err, DxfError::Io(_)));
}
}