use std::fmt;
use std::str::FromStr;
use crate::ast::Expr;
use crate::check::{SemanticError, check, check_any};
use crate::eval::error::EvalError;
use crate::eval::{Environment, Focus, evaluate};
use crate::model::{FhirType, Model};
use crate::parse::{ParseError, parse};
use crate::value::Collection;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Expression {
source: String,
tree: Expr,
}
impl Expression {
pub fn parse(text: &str) -> Result<Self, ParseError> {
Ok(Self {
source: String::from(text),
tree: parse(text)?,
})
}
#[must_use]
pub fn source(&self) -> &str {
&self.source
}
#[must_use]
pub const fn tree(&self) -> &Expr {
&self.tree
}
pub fn check(&self, model: &dyn Model, context: FhirType) -> Result<(), SemanticError> {
check(&self.tree, model, context)
}
pub fn check_any(&self, model: &dyn Model, contexts: &[FhirType]) -> Result<(), SemanticError> {
check_any(&self.tree, model, contexts)
}
pub fn evaluate<'a>(
&self,
env: &Environment<'a>,
focus: Focus<'a>,
) -> Result<Collection<'a>, EvalError> {
evaluate(&self.tree, env, focus)
}
}
impl FromStr for Expression {
type Err = ParseError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
Self::parse(text)
}
}
impl fmt::Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.source)
}
}