use std::{error::Error, fmt, sync::Arc};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MaterialErrorKind {
Schema,
Syntax,
Validation,
Forbidden,
Limit,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MaterialError {
kind: MaterialErrorKind,
label: Arc<str>,
message: String,
line: Option<u32>,
column: Option<u32>,
}
impl MaterialError {
pub(crate) fn new(kind: MaterialErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
label: Arc::from(""),
message: message.into(),
line: None,
column: None,
}
}
pub(crate) fn at(mut self, line: u32, column: u32) -> Self {
self.line = Some(line);
self.column = Some(column);
self
}
pub(crate) fn labeled(mut self, label: &Arc<str>) -> Self {
self.label = Arc::clone(label);
self
}
pub fn kind(&self) -> MaterialErrorKind {
self.kind
}
pub fn label(&self) -> &str {
&self.label
}
pub fn message(&self) -> &str {
&self.message
}
pub fn line(&self) -> Option<u32> {
self.line
}
pub fn column(&self) -> Option<u32> {
self.column
}
}
impl fmt::Display for MaterialError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "material")?;
if !self.label.is_empty() {
write!(f, " `{}`", self.label)?;
}
if let (Some(line), Some(column)) = (self.line, self.column) {
write!(f, " (line {line}, column {column})")?;
}
write!(f, ": {}", self.message)
}
}
impl Error for MaterialError {}