use std::fmt;
use thiserror::Error;
use crate::parser::Rule;
#[derive(Debug, Error)]
pub enum MiniConfError {
#[error("parse error: {0}")]
Pest(Box<pest::error::Error<Rule>>),
#[error("{kind} on line {line}: {message}")]
Semantic {
kind: ParseErrorKind,
line: usize,
message: String,
},
}
impl MiniConfError {
pub(crate) fn semantic(kind: ParseErrorKind, line: usize, message: impl Into<String>) -> Self {
Self::Semantic {
kind,
line,
message: message.into(),
}
}
}
impl From<pest::error::Error<Rule>> for MiniConfError {
fn from(value: pest::error::Error<Rule>) -> Self {
Self::Pest(Box::new(value))
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ParseErrorKind {
DuplicateKey,
InvalidValue,
}
impl fmt::Display for ParseErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DuplicateKey => write!(f, "duplicate key"),
Self::InvalidValue => write!(f, "invalid value"),
}
}
}