use std::io;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Unknown file format: not a valid PDF")]
UnknownFormat,
#[error("Unsupported PDF version: {0}")]
UnsupportedVersion(String),
#[error("PDF parsing error: {0}")]
PdfParse(String),
#[error("Document is encrypted")]
Encrypted,
#[error("Invalid password")]
InvalidPassword,
#[error("Corrupted PDF structure: {0}")]
Corrupted(String),
#[error("Missing required object: {0}")]
MissingObject(String),
#[error("Font decoding error: {0}")]
FontDecode(String),
#[error("Image extraction error: {0}")]
ImageExtract(String),
#[error("Rendering error: {0}")]
Render(String),
#[error("Text extraction error: {0}")]
TextExtract(String),
#[error("Page {0} is out of range (document has {1} pages)")]
PageOutOfRange(u32, u32),
#[error("Invalid page range: {0}")]
InvalidPageRange(String),
#[error("Resource not found: {0}")]
ResourceNotFound(String),
#[error("Encoding error: {0}")]
Encoding(String),
#[error("{0}")]
Other(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = Error::Encrypted;
assert_eq!(err.to_string(), "Document is encrypted");
let err = Error::PageOutOfRange(10, 5);
assert_eq!(
err.to_string(),
"Page 10 is out of range (document has 5 pages)"
);
}
#[test]
fn test_io_error_conversion() {
let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
let err: Error = io_err.into();
assert!(matches!(err, Error::Io(_)));
}
}