use std::io;
use std::path::PathBuf;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum TextconError {
#[error("invalid configuration: {0}")]
Config(String),
#[error("template byte {offset}: {message}")]
TemplateSyntax { offset: u64, message: String },
#[error("sandbox denied reference {path}: {reason}")]
SandboxDenied { path: PathBuf, reason: String },
#[error("unsupported filesystem object: {path}")]
UnsupportedFileType { path: PathBuf },
#[error("cannot {operation} {path}: {source}")]
PathIo {
operation: &'static str,
path: PathBuf,
#[source]
source: io::Error,
},
#[error("cannot read {name}: {source}")]
Input {
name: String,
#[source]
source: io::Error,
},
#[error("cannot write output: {0}")]
Output(#[source] io::Error),
#[error("invalid ignore rule in {origin}: {message}")]
Ignore { origin: String, message: String },
}
impl TextconError {
pub(crate) fn path_io(
operation: &'static str,
path: impl Into<PathBuf>,
source: io::Error,
) -> Self {
Self::PathIo {
operation,
path: path.into(),
source,
}
}
pub(crate) const fn output(source: io::Error) -> Self {
Self::Output(source)
}
#[must_use]
pub fn is_output_broken_pipe(&self) -> bool {
matches!(self, Self::Output(error) if error.kind() == io::ErrorKind::BrokenPipe)
}
}
pub type Result<T> = std::result::Result<T, TextconError>;