use std::error::Error;
use std::fmt;
pub type Result<T> = std::result::Result<T, PixelFlowError>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ErrorCategory {
Core,
Graph,
Script,
Plugin,
Source,
Format,
Io,
Internal,
}
impl ErrorCategory {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Core => "core",
Self::Graph => "graph",
Self::Script => "script",
Self::Plugin => "plugin",
Self::Source => "source",
Self::Format => "format",
Self::Io => "io",
Self::Internal => "internal",
}
}
}
impl fmt::Display for ErrorCategory {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ErrorCode(&'static str);
impl ErrorCode {
#[must_use]
pub const fn new(code: &'static str) -> Self {
Self(code)
}
#[must_use]
pub const fn as_str(self) -> &'static str {
self.0
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.0)
}
}
#[derive(Debug)]
pub struct PixelFlowError {
category: ErrorCategory,
code: ErrorCode,
message: String,
}
impl PixelFlowError {
#[must_use]
pub fn new(category: ErrorCategory, code: ErrorCode, message: impl Into<String>) -> Self {
Self {
category,
code,
message: message.into(),
}
}
#[must_use]
pub const fn category(&self) -> ErrorCategory {
self.category
}
#[must_use]
pub const fn code(&self) -> ErrorCode {
self.code
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for PixelFlowError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{} error {}: {}",
self.category, self.code, self.message
)
}
}
impl Error for PixelFlowError {}