1use std::error::Error;
4use std::fmt;
5
6pub type Result<T> = std::result::Result<T, PixelFlowError>;
8
9#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum ErrorCategory {
12 Core,
14 Graph,
16 Script,
18 Plugin,
20 Source,
22 Format,
24 Io,
26 Internal,
28}
29
30impl ErrorCategory {
31 #[must_use]
33 pub const fn as_str(self) -> &'static str {
34 match self {
35 Self::Core => "core",
36 Self::Graph => "graph",
37 Self::Script => "script",
38 Self::Plugin => "plugin",
39 Self::Source => "source",
40 Self::Format => "format",
41 Self::Io => "io",
42 Self::Internal => "internal",
43 }
44 }
45}
46
47impl fmt::Display for ErrorCategory {
48 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
49 formatter.write_str(self.as_str())
50 }
51}
52
53#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
55pub struct ErrorCode(&'static str);
56
57impl ErrorCode {
58 #[must_use]
60 pub const fn new(code: &'static str) -> Self {
61 Self(code)
62 }
63
64 #[must_use]
66 pub const fn as_str(self) -> &'static str {
67 self.0
68 }
69}
70
71impl fmt::Display for ErrorCode {
72 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
73 formatter.write_str(self.0)
74 }
75}
76
77#[derive(Debug)]
79pub struct PixelFlowError {
80 category: ErrorCategory,
81 code: ErrorCode,
82 message: String,
83}
84
85impl PixelFlowError {
86 #[must_use]
88 pub fn new(category: ErrorCategory, code: ErrorCode, message: impl Into<String>) -> Self {
89 Self {
90 category,
91 code,
92 message: message.into(),
93 }
94 }
95
96 #[must_use]
98 pub const fn category(&self) -> ErrorCategory {
99 self.category
100 }
101
102 #[must_use]
104 pub const fn code(&self) -> ErrorCode {
105 self.code
106 }
107
108 #[must_use]
110 pub fn message(&self) -> &str {
111 &self.message
112 }
113}
114
115impl fmt::Display for PixelFlowError {
116 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
117 write!(
118 formatter,
119 "{} error {}: {}",
120 self.category, self.code, self.message
121 )
122 }
123}
124
125impl Error for PixelFlowError {}