Skip to main content

gmac/
error.rs

1use core::fmt;
2use std::io;
3
4pub type Result<T> = core::result::Result<T, Error>;
5
6#[derive(Debug)]
7pub enum Error {
8    Custom(String),
9
10    // Core
11    MeshGeneration(String),
12
13    // Morph
14    Deformation(String),
15
16    // Io
17    FileSystem(io::Error),
18}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        match self {
23            Error::Custom(msg) => write!(f, "🔴 Error: {msg}"),
24            Error::MeshGeneration(msg) => write!(f, "🔴 Mesh generation error: {msg}"),
25            Error::Deformation(msg) => write!(f, "🔴 Deformation error: {msg}"),
26            Error::FileSystem(err) => write!(f, "🔴 IO error: {err}"),
27        }
28    }
29}
30
31// Implementing the standard Error trait for better ecosystem compatibility.
32impl std::error::Error for Error {
33    // The `source` method provides the underlying cause of the error, if any.
34    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35        match self {
36            // These variants don't wrap another error, so they have no source.
37            Error::Custom(_) => None,
38            Error::MeshGeneration(_) => None,
39            Error::Deformation(_) => None,
40            Error::FileSystem(err) => Some(err),
41        }
42    }
43}
44
45impl From<&str> for Error {
46    fn from(value: &str) -> Self {
47        Self::Custom(value.to_string())
48    }
49}
50
51impl From<String> for Error {
52    fn from(value: String) -> Self {
53        Self::Custom(value)
54    }
55}
56
57impl From<io::Error> for Error {
58    fn from(err: io::Error) -> Self {
59        Self::FileSystem(err)
60    }
61}