Skip to main content

document_svg/
error.rs

1use std::fmt::{Display, Formatter};
2use std::io;
3
4#[derive(Debug)]
5pub enum Error {
6    Io(io::Error),
7    InvalidInput(String),
8    Unsupported(String),
9    LimitExceeded(String),
10    Pdf(lopdf::Error),
11    Xml(quick_xml::Error),
12    Zip(zip::result::ZipError),
13    Json(serde_json::Error),
14}
15
16impl Display for Error {
17    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::Io(error) => write!(formatter, "I/O error: {error}"),
20            Self::InvalidInput(message) => write!(formatter, "invalid input: {message}"),
21            Self::Unsupported(message) => write!(formatter, "unsupported input: {message}"),
22            Self::LimitExceeded(message) => write!(formatter, "safety limit exceeded: {message}"),
23            Self::Pdf(error) => write!(formatter, "PDF error: {error}"),
24            Self::Xml(error) => write!(formatter, "XML error: {error}"),
25            Self::Zip(error) => write!(formatter, "ZIP error: {error}"),
26            Self::Json(error) => write!(formatter, "JSON error: {error}"),
27        }
28    }
29}
30
31impl std::error::Error for Error {}
32
33impl From<io::Error> for Error {
34    fn from(value: io::Error) -> Self {
35        Self::Io(value)
36    }
37}
38
39impl From<lopdf::Error> for Error {
40    fn from(value: lopdf::Error) -> Self {
41        Self::Pdf(value)
42    }
43}
44
45impl From<quick_xml::Error> for Error {
46    fn from(value: quick_xml::Error) -> Self {
47        Self::Xml(value)
48    }
49}
50
51impl From<zip::result::ZipError> for Error {
52    fn from(value: zip::result::ZipError) -> Self {
53        Self::Zip(value)
54    }
55}
56
57impl From<serde_json::Error> for Error {
58    fn from(value: serde_json::Error) -> Self {
59        Self::Json(value)
60    }
61}
62
63pub type Result<T> = std::result::Result<T, Error>;