use serde_json::Error as JsonError;
use std::io::Error as IoError;
use std::{num::ParseIntError, time::SystemTimeError};
use thiserror::Error as ThisError;
#[derive(ThisError, Debug)]
pub enum ErrorType {
#[error("JSON error: \"{0}\"")]
Json(JsonError),
#[error("IO error: \"{0}\"")]
Io(IoError),
#[error("Shell {0}")]
Subprocess(String),
#[error("System time error: \"{0}\"")]
SystemTime(SystemTimeError),
#[error("Expected key-value pair")]
ExpectedKeyValuePair,
#[error("Expected closing front matter delimiter")]
ExpectedClosingDelimiter,
#[error("Front matter already exists")]
FrontMatterExists,
#[error("Unknown instruction")]
UnknownInstruction,
#[error("Malformed instruction")]
MalformedInstruction,
#[error("Expected continuation")]
ExpectedContinuation,
#[error("Unexpected continuation")]
UnexpectedContinuation,
#[error("Unknown configuration")]
UnknownConfig,
#[error("Unknown front matter")]
UnknownFrontMatter,
#[error("Header already written")]
HeaderAlreadyWritten,
}
impl ErrorType {
#[must_use]
pub const fn with_line(self, line: usize) -> Error {
Error { error: self, line }
}
}
impl From<JsonError> for ErrorType {
fn from(error: JsonError) -> Self {
Self::Json(error)
}
}
impl From<IoError> for ErrorType {
fn from(error: IoError) -> Self {
Self::Io(error)
}
}
impl From<ParseIntError> for ErrorType {
fn from(_: ParseIntError) -> Self {
Self::MalformedInstruction
}
}
impl From<SystemTimeError> for ErrorType {
fn from(error: SystemTimeError) -> Self {
Self::SystemTime(error)
}
}
#[cfg(test)]
impl PartialEq for ErrorType {
fn eq(&self, other: &Self) -> bool {
format!("{self:?}") == format!("{other:?}")
}
}
#[cfg_attr(test, derive(PartialEq))]
#[derive(ThisError, Debug)]
#[error("{error} at line {line}")]
pub struct Error {
pub error: ErrorType,
pub line: usize,
}