noctua/core/src/error.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
// src/error.rs
//
// Central error and result types for noctua_core.
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
/// A requested file or resource was not found.
NotFound(String),
/// The file format or operation is not supported.
Unsupported(String),
/// A rendering operation failed.
Render(String),
/// A session could not be loaded or saved.
Session(String),
/// An underlying I/O error.
Io(std::io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::NotFound(msg) => write!(f, "Not found: {msg}"),
Error::Unsupported(msg) => write!(f, "Unsupported: {msg}"),
Error::Render(msg) => write!(f, "Render error: {msg}"),
Error::Session(msg) => write!(f, "Session error: {msg}"),
Error::Io(err) => write!(f, "I/O error: {err}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}