use std::path::{Path, PathBuf};
use crate::diagnostics::Diagnostic;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{0}")]
Diagnostic(Box<Diagnostic>),
#[error("{path}: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("{0}")]
Config(String),
#[error("{0}")]
Message(String),
}
impl Error {
pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
Error::Io {
path: path.into(),
source,
}
}
pub fn config(msg: impl Into<String>) -> Self {
Error::Config(msg.into())
}
pub fn message(msg: impl Into<String>) -> Self {
Error::Message(msg.into())
}
pub fn diagnostic(&self) -> Option<&Diagnostic> {
match self {
Error::Diagnostic(d) => Some(d),
_ => None,
}
}
}
impl From<Diagnostic> for Error {
fn from(d: Diagnostic) -> Self {
Error::Diagnostic(Box::new(d))
}
}
pub(crate) fn io_at<T>(path: &Path, r: std::io::Result<T>) -> Result<T> {
r.map_err(|e| Error::io(path, e))
}