shedron 0.1.4

A DSL for dice rolls
Documentation
use crate::{ast::Ops, interpret::OutputValues};

#[derive(Debug)]
/// A runtime error, to be used during interpretation
pub struct RunError {
    message: String,
}

impl RunError {
    pub fn new(message: impl ToString) -> Self {
        Self {
            message: message.to_string(),
        }
    }
}

impl RunError {
    pub fn argument(op: &Ops, l: OutputValues, r: OutputValues) -> Self {
        Self {
            message: format!("Unexpected values: `{}` cannot process arguments `{:?}` as left argument and `{:?}` and right argument.", op.name(), l, r),
        }
    }
}

impl std::fmt::Display for RunError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "RunError: {}", self.message)
    }
}

impl std::error::Error for RunError {}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
/// A parsing-related error, to be used during parsing
pub struct ParseError {
    message: String,
    pos: (usize, usize),
}

impl ParseError {
    pub fn new(message: impl ToString, pos: (usize, usize)) -> Self {
        Self {
            message: message.to_string(),
            pos,
        }
    }
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let pos = (self.pos.0 + 1, self.pos.1 + 1);
        write!(f, "ParseError @ {}:{} - {}", pos.0, pos.1, self.message)
    }
}

impl std::error::Error for ParseError {}