torment-nexus/core/src/error.rs
2025-07-09 21:56:51 -04:00

113 lines
2.7 KiB
Rust

use std::{error::Error as StdError, fmt::Display, path::PathBuf};
use toml::de::Error as TomlError;
#[derive(Debug)]
pub struct ShellExpansionError {
variable: String,
context: Option<String>,
}
impl ShellExpansionError {
pub fn new(variable: &str, context: Option<&str>) -> Self {
Self {
variable: variable.into(),
context: context.map(Into::into),
}
}
}
impl Display for ShellExpansionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(context) = &self.context {
write!(f, "Error expanding {} in {}", self.variable, context)
} else {
write!(f, "Error expanding {}", self.variable)
}
}
}
#[derive(Debug)]
pub struct FileError {
path: PathBuf,
source: Box<dyn StdError>,
}
impl FileError {
pub fn new(path: impl Into<PathBuf>, source: impl Into<Box<dyn StdError>>) -> Self {
Self {
path: path.into(),
source: source.into(),
}
}
}
impl Display for FileError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Error handling \"{}\": {}",
self.path.display(),
&self.source
)
}
}
impl std::error::Error for FileError {}
#[derive(Debug)]
pub enum ProjectManifestError {
Read(FileError),
Parse(TomlError),
}
impl From<FileError> for ProjectManifestError {
fn from(value: FileError) -> Self {
Self::Read(value)
}
}
impl From<TomlError> for ProjectManifestError {
fn from(value: TomlError) -> Self {
Self::Parse(value)
}
}
impl std::fmt::Display for ProjectManifestError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Read(error) => write!(f, "Error reading project manifest: {error}"),
Self::Parse(error) => write!(f, "Error parsing project manifest: {error}"),
}
}
}
impl std::error::Error for ProjectManifestError {}
#[derive(Debug)]
pub enum Error {
LoadConfig(confique::Error),
ProjectManifest(ProjectManifestError),
}
impl From<confique::Error> for Error {
fn from(value: confique::Error) -> Self {
Self::LoadConfig(value)
}
}
impl From<ProjectManifestError> for Error {
fn from(value: ProjectManifestError) -> Self {
Self::ProjectManifest(value)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::LoadConfig(error) => write!(f, "Couldn't load config: {error}"),
Error::ProjectManifest(error) => write!(f, "Couldn't read projects: {error}"),
}
}
}
impl std::error::Error for Error {}