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)??; 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)
}
#[derive(Debug, Error, PartialEq)]
pub enum Error {
#[error("parse error: {0}")]
Parse(#[from] ParseError),
#[error("evaluation error: {0}")]
Eval(#[from] EvalError),
}