1use tytanic_filter::ast::Id;
8use tytanic_filter::eval::{self, Context, Error, Func, Set, Value};
9
10use crate::test::Test;
11
12impl eval::Test for Test {
13 fn id(&self) -> &str {
14 self.id().as_str()
15 }
16}
17
18pub fn context() -> Context<Test> {
21 type FuncPtr =
22 for<'a, 'b> fn(&'a Context<Test>, &'b [Value<Test>]) -> Result<Value<Test>, Error>;
23
24 let mut ctx = Context::new();
25
26 let functions = [
27 ("all", built_in::all_ctor as FuncPtr),
28 ("none", built_in::none_ctor),
29 ("skip", built_in::skip_ctor),
30 ("compile-only", built_in::compile_only_ctor),
31 ("ephemeral", built_in::ephemeral_ctor),
32 ("persistent", built_in::persistent_ctor),
33 ];
34
35 for (id, func) in functions {
36 ctx.bind(Id(id.into()), Value::Func(Func::new(func)));
37 }
38
39 ctx
40}
41
42pub mod built_in {
45 use tytanic_filter::eval::{Context, Error, Func, Value};
46
47 use super::*;
48
49 pub fn all_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
51 Func::expect_no_args("all", ctx, args)?;
52 Ok(Value::Set(all()))
53 }
54
55 pub fn all() -> Set<Test> {
58 Set::new(|_, _| Ok(true))
59 }
60
61 pub fn none_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
63 Func::expect_no_args("none", ctx, args)?;
64 Ok(Value::Set(none()))
65 }
66
67 pub fn none() -> Set<Test> {
70 Set::new(|_, _| Ok(false))
71 }
72
73 pub fn skip_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
75 Func::expect_no_args("skip", ctx, args)?;
76 Ok(Value::Set(skip()))
77 }
78
79 pub fn skip() -> Set<Test> {
82 Set::new(|_, test: &Test| Ok(test.is_skip()))
83 }
84
85 pub fn compile_only_ctor(
87 ctx: &Context<Test>,
88 args: &[Value<Test>],
89 ) -> Result<Value<Test>, Error> {
90 Func::expect_no_args("compile-only", ctx, args)?;
91 Ok(Value::Set(compile_only()))
92 }
93
94 pub fn compile_only() -> Set<Test> {
97 Set::new(|_, test: &Test| Ok(test.kind().is_compile_only()))
98 }
99
100 pub fn ephemeral_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
102 Func::expect_no_args("ephemeral", ctx, args)?;
103 Ok(Value::Set(ephemeral()))
104 }
105
106 pub fn ephemeral() -> Set<Test> {
109 Set::new(|_, test: &Test| Ok(test.kind().is_ephemeral()))
110 }
111
112 pub fn persistent_ctor(
114 ctx: &Context<Test>,
115 args: &[Value<Test>],
116 ) -> Result<Value<Test>, Error> {
117 Func::expect_no_args("persistent", ctx, args)?;
118 Ok(Value::Set(persistent()))
119 }
120
121 pub fn persistent() -> Set<Test> {
124 Set::new(|_, test: &Test| Ok(test.kind().is_persistent()))
125 }
126}