fret_runtime/when_expr/
mod.rs1mod ast;
2mod eval;
3mod parser;
4mod validate;
5
6#[cfg(test)]
7mod tests;
8
9use std::sync::Arc;
10
11use crate::InputContext;
12
13use ast::Expr;
14
15#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16pub struct WhenExpr(Expr);
17
18#[derive(Debug, Clone, Copy)]
23pub struct WhenEvalContext<'a> {
24 pub input: &'a InputContext,
25 pub key_contexts: &'a [Arc<str>],
26}
27
28impl<'a> WhenEvalContext<'a> {
29 pub fn new(input: &'a InputContext) -> Self {
30 Self {
31 input,
32 key_contexts: &[],
33 }
34 }
35
36 pub fn with_key_contexts(input: &'a InputContext, key_contexts: &'a [Arc<str>]) -> Self {
37 Self {
38 input,
39 key_contexts,
40 }
41 }
42}
43
44impl WhenExpr {
45 pub fn parse(input: &str) -> Result<Self, String> {
46 Ok(Self(parser::parse(input)?))
47 }
48
49 pub fn validate(&self) -> Result<(), WhenExprValidationError> {
50 self.0.validate_bool_expr()
51 }
52
53 pub fn eval(&self, ctx: &InputContext) -> bool {
54 self.eval_in(&WhenEvalContext::new(ctx))
55 }
56
57 pub fn eval_with_key_contexts(&self, ctx: &InputContext, key_contexts: &[Arc<str>]) -> bool {
58 self.eval_in(&WhenEvalContext::with_key_contexts(ctx, key_contexts))
59 }
60
61 pub fn eval_in(&self, cx: &WhenEvalContext<'_>) -> bool {
62 self.0.eval(cx)
63 }
64}
65
66pub use validate::{WhenExprValidationError, WhenValueKind};