use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Position {
pub line: usize,
pub column: usize,
}
macro_rules! error_type {
($(#[$doc:meta])* $name:ident, $variant:ident $(, $field:ident: $type:ty)*) => {
$(#[$doc])*
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct $name {
reason: String,
message: String,
position: Option<Position>,
$($field: Option<$type>,)*
}
impl $name {
#[doc = concat!("use predicator_rs::{", stringify!($name), ", Position};")]
///
#[doc = concat!("let error = ", stringify!($name), "::new(\"some_reason\", \"a sentence\");")]
pub fn new(reason: impl Into<String>, message: impl Into<String>) -> Self {
Self {
reason: reason.into(),
message: message.into(),
position: None,
$($field: None,)*
}
}
#[doc = concat!("use predicator_rs::{", stringify!($name), ", Position};")]
#[doc = concat!("let error = ", stringify!($name), "::new(\"some_reason\", \"a sentence\");")]
#[must_use]
pub fn with_position(mut self, position: Position) -> Self {
self.position = Some(position);
self
}
#[doc = concat!("use predicator_rs::{", stringify!($name), ", Position};")]
#[doc = concat!("let error = ", stringify!($name), "::new(\"some_reason\", \"a sentence\");")]
pub fn reason(&self) -> &str {
&self.reason
}
#[doc = concat!("use predicator_rs::{", stringify!($name), ", Position};")]
#[doc = concat!("let error = ", stringify!($name), "::new(\"some_reason\", \"a sentence\");")]
pub fn message(&self) -> &str {
&self.message
}
#[doc = concat!("use predicator_rs::{", stringify!($name), ", Position};")]
#[doc = concat!("let error = ", stringify!($name), "::new(\"some_reason\", \"a sentence\");")]
pub fn position(&self) -> Option<Position> {
self.position
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for $name {}
impl From<$name> for Error {
fn from(error: $name) -> Error {
Error::$variant(error)
}
}
};
}
error_type! {
ParseError, Parse
}
error_type! {
EvaluationError, Evaluation, root: String
}
impl EvaluationError {
pub fn root(&self) -> Option<&str> {
self.root.as_deref()
}
pub(crate) fn with_root(mut self, root: impl Into<String>) -> Self {
self.root = Some(root.into());
self
}
}
error_type! {
TypeMismatchError, TypeMismatch
}
error_type! {
UndefinedVariableError, UndefinedVariable
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Parse(ParseError),
Evaluation(EvaluationError),
TypeMismatch(TypeMismatchError),
UndefinedVariable(UndefinedVariableError),
}
impl Error {
pub fn type_name(&self) -> &'static str {
match self {
Error::Parse(_) => "ParseError",
Error::Evaluation(_) => "EvaluationError",
Error::TypeMismatch(_) => "TypeMismatchError",
Error::UndefinedVariable(_) => "UndefinedVariableError",
}
}
pub fn reason(&self) -> &str {
match self {
Error::Parse(error) => error.reason(),
Error::Evaluation(error) => error.reason(),
Error::TypeMismatch(error) => error.reason(),
Error::UndefinedVariable(error) => error.reason(),
}
}
pub fn message(&self) -> &str {
match self {
Error::Parse(error) => error.message(),
Error::Evaluation(error) => error.message(),
Error::TypeMismatch(error) => error.message(),
Error::UndefinedVariable(error) => error.message(),
}
}
pub fn position(&self) -> Option<Position> {
match self {
Error::Parse(error) => error.position(),
Error::Evaluation(error) => error.position(),
Error::TypeMismatch(error) => error.position(),
Error::UndefinedVariable(error) => error.position(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl std::error::Error for Error {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_variant_carries_its_corpus_type_reason_message_and_position() {
let at = Position { line: 2, column: 5 };
let errors = [
(
Error::from(ParseError::new("p", "parse").with_position(at)),
"ParseError",
"p",
"parse",
Some(at),
),
(
Error::from(EvaluationError::new("e", "evaluation")),
"EvaluationError",
"e",
"evaluation",
None,
),
(
Error::from(TypeMismatchError::new("t", "mismatch").with_position(at)),
"TypeMismatchError",
"t",
"mismatch",
Some(at),
),
(
Error::from(UndefinedVariableError::new("u", "unbound")),
"UndefinedVariableError",
"u",
"unbound",
None,
),
];
for (error, type_name, reason, message, position) in errors {
assert_eq!(error.type_name(), type_name);
assert_eq!(error.reason(), reason);
assert_eq!(error.message(), message);
assert_eq!(error.to_string(), message);
assert_eq!(error.position(), position);
let source: &dyn std::error::Error = &error;
assert!(source.source().is_none());
}
}
}