expy 0.0.2

Embeddable & extensible expression evaluator
Documentation
//! Facade interface to the library.

use thiserror::Error;

use super::eval::{Context, Error as EvalError};
use super::parser::{AstHandler, Driver, EvalHandler, Error as ParseError, Format, FormatHandler};
use super::model::{Expr, Value};


pub fn parse(input: impl AsRef<str>) -> Result<Expr, ParseError> {
    Driver::new(AstHandler).process(input)
}


pub fn eval(input: impl AsRef<str>) -> Result<Value, Error> {
    let mut context = Context::new();
    eval_in(&mut context, input)
}

pub fn eval_in(context: &mut Context, input: impl AsRef<str>) -> Result<Value, Error> {
    let handler = EvalHandler::new(context);
    let value = Driver::new(handler).process(input)??;  // yep
    Ok(value)
}


pub fn prettify(input: impl AsRef<str>) -> Result<String, ParseError> {
    Driver::new(FormatHandler::new(Format::Pretty)).process(input)
}

pub fn minify(input: impl AsRef<str>) -> Result<String, ParseError> {
    Driver::new(FormatHandler::new(Format::Minified)).process(input)
}


/// Error while processing an expression.
#[derive(Debug, Error, PartialEq)]
pub enum Error {
    #[error("parse error: {0}")]
    Parse(#[from] ParseError),

    #[error("evaluation error: {0}")]
    Eval(#[from] EvalError),
}