Skip to main content

maple_render_core/
error.rs

1use std::path::PathBuf;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug)]
6pub enum Error {
7    Io(std::io::Error),
8    Image(image::ImageError),
9    Zip(zip::result::ZipError),
10    Json(serde_json::Error),
11    MissingFile(String),
12    MissingData(String),
13    InvalidTemplate(String),
14    FileNotFound(PathBuf),
15    NoRenders,
16    NoMapping,
17    NoRepository,
18    NoInputs,
19    GifEncode(String),
20    WebpEncode(String),
21    VideoEncode(String),
22    TextRender(String),
23    Other(String),
24}
25
26impl std::fmt::Display for Error {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        match self {
29            Error::Io(e) => write!(f, "IO error: {}", e),
30            Error::Image(e) => write!(f, "Image error: {}", e),
31            Error::Zip(e) => write!(f, "Zip error: {}", e),
32            Error::Json(e) => write!(f, "JSON error: {}", e),
33            Error::MissingFile(name) => write!(f, "Missing file: {}", name),
34            Error::MissingData(msg) => write!(f, "Missing data: {}", msg),
35            Error::InvalidTemplate(msg) => write!(f, "Invalid template: {}", msg),
36            Error::FileNotFound(path) => write!(f, "File not found: {}", path.display()),
37            Error::NoRenders => write!(f, "No renders attached"),
38            Error::NoMapping => write!(f, "No mapping attached"),
39            Error::NoRepository => write!(f, "No repository attached"),
40            Error::NoInputs => write!(f, "No inputs attached"),
41            Error::GifEncode(msg) => write!(f, "GIF encoding error: {}", msg),
42            Error::WebpEncode(msg) => write!(f, "WebP encoding error: {}", msg),
43            Error::VideoEncode(msg) => write!(f, "Video encoding error: {}", msg),
44            Error::TextRender(msg) => write!(f, "Text rendering error: {}", msg),
45            Error::Other(msg) => write!(f, "{}", msg),
46        }
47    }
48}
49
50impl std::error::Error for Error {
51    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
52        match self {
53            Error::Io(e) => Some(e),
54            Error::Image(e) => Some(e),
55            Error::Zip(e) => Some(e),
56            Error::Json(e) => Some(e),
57            _ => None,
58        }
59    }
60}
61
62impl From<std::io::Error> for Error {
63    fn from(e: std::io::Error) -> Self {
64        Error::Io(e)
65    }
66}
67
68impl From<image::ImageError> for Error {
69    fn from(e: image::ImageError) -> Self {
70        Error::Image(e)
71    }
72}
73
74impl From<zip::result::ZipError> for Error {
75    fn from(e: zip::result::ZipError) -> Self {
76        Error::Zip(e)
77    }
78}
79
80impl From<serde_json::Error> for Error {
81    fn from(e: serde_json::Error) -> Self {
82        Error::Json(e)
83    }
84}
85
86impl From<String> for Error {
87    fn from(s: String) -> Self {
88        Error::Other(s)
89    }
90}
91
92impl From<&str> for Error {
93    fn from(s: &str) -> Self {
94        Error::Other(s.to_string())
95    }
96}