Skip to main content

tyt_material/
error.rs

1use std::{
2    error::Error as StdError,
3    fmt::{Display, Formatter, Result as FmtResult},
4    io::Error as IOError,
5};
6use tyt_common::ExecFailed;
7
8/// An error from a material operation.
9#[derive(Debug)]
10pub enum Error {
11    Glob(String),
12    Magick(ExecFailed),
13    IO(IOError),
14}
15
16impl Display for Error {
17    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
18        match self {
19            Error::Glob(msg) => write!(f, "{msg}"),
20            Error::Magick(ExecFailed {
21                exit_code,
22                stdout,
23                stderr,
24            }) => {
25                match exit_code {
26                    Some(code) => write!(f, "magick exited with code {code}")?,
27                    None => write!(f, "magick killed by signal")?,
28                }
29                if !stdout.is_empty() {
30                    write!(f, "\nstdout:\n{stdout}")?;
31                }
32                if !stderr.is_empty() {
33                    write!(f, "\nstderr:\n{stderr}")?;
34                }
35                Ok(())
36            }
37            Error::IO(e) => e.fmt(f),
38        }
39    }
40}
41
42impl StdError for Error {
43    fn source(&self) -> Option<&(dyn StdError + 'static)> {
44        match self {
45            Error::Glob(_) | Error::Magick(_) => None,
46            Error::IO(e) => Some(e),
47        }
48    }
49}
50
51impl From<IOError> for Error {
52    fn from(e: IOError) -> Self {
53        Error::IO(e)
54    }
55}