Skip to main content

dsfb_computer_graphics/
error.rs

1use std::error::Error as StdError;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug)]
5pub enum Error {
6    Io(std::io::Error),
7    Image(image::ImageError),
8    SerdeJson(serde_json::Error),
9    Message(String),
10}
11
12impl Display for Error {
13    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14        match self {
15            Self::Io(error) => write!(f, "I/O error: {error}"),
16            Self::Image(error) => write!(f, "image error: {error}"),
17            Self::SerdeJson(error) => write!(f, "serde_json error: {error}"),
18            Self::Message(message) => f.write_str(message),
19        }
20    }
21}
22
23impl StdError for Error {}
24
25impl From<std::io::Error> for Error {
26    fn from(value: std::io::Error) -> Self {
27        Self::Io(value)
28    }
29}
30
31impl From<image::ImageError> for Error {
32    fn from(value: image::ImageError) -> Self {
33        Self::Image(value)
34    }
35}
36
37impl From<serde_json::Error> for Error {
38    fn from(value: serde_json::Error) -> Self {
39        Self::SerdeJson(value)
40    }
41}
42
43pub type Result<T> = std::result::Result<T, Error>;