tytanic_filter/ast/
atom.rs

1use super::Id;
2use super::Num;
3use super::Pat;
4use super::Str;
5use crate::eval::Context;
6use crate::eval::Error;
7use crate::eval::Eval;
8use crate::eval::Test;
9use crate::eval::Value;
10
11/// A leaf node within a test set expression such as an identifier or literal.
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub enum Atom {
14    /// A variable.
15    Id(Id),
16
17    /// A number literal.
18    Num(Num),
19
20    /// A string literal.
21    Str(Str),
22
23    /// A pattern literal.
24    Pat(Pat),
25}
26
27impl<T: Test> Eval<T> for Atom {
28    fn eval(&self, ctx: &Context<T>) -> Result<Value<T>, Error> {
29        Ok(match self {
30            Self::Id(id) => id.eval(ctx)?,
31            Self::Num(n) => Value::Num(*n),
32            Self::Str(s) => Value::Str(s.clone()),
33            Self::Pat(pat) => pat.eval(ctx)?,
34        })
35    }
36}