use super::{Ast, FunctionArgs, FunctionName, VariableName, VariableValue};
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum Node {
Boolean(bool),
DateTime(crate::DateTime),
Float(f64),
Function {
args: FunctionArgs,
body: Ast,
name: FunctionName,
},
FunctionCall { args: Vec<Ast>, name: FunctionName },
InfixFunctionCall {
left: Ast,
operator: FunctionName,
right: Ast,
},
Integer(i64),
Reference(String),
Text(String),
Variable {
name: VariableName,
value: VariableValue,
},
}
impl Node {
#[cfg(test)]
pub(crate) fn fn_def<N, A>(name: N, args: &[&str], body: A) -> Self
where
N: Into<String>,
A: Into<Ast>,
{
Self::Function {
name: name.into(),
args: args.iter().map(|a| a.to_string()).collect(),
body: body.into(),
}
}
#[cfg(test)]
pub(crate) fn fn_call<N, A>(name: N, args: &[A]) -> Self
where
N: Into<String>,
A: Into<Ast> + Clone,
{
Self::FunctionCall {
name: name.into(),
args: args.iter().map(|a| (*a).clone().into()).collect(),
}
}
#[cfg(test)]
pub(crate) fn infix_fn_call<L, O, R>(left: L, operator: O, right: R) -> Self
where
L: Into<Ast>,
O: Into<String>,
R: Into<Ast>,
{
Self::InfixFunctionCall {
left: left.into(),
operator: operator.into(),
right: right.into(),
}
}
pub(crate) fn reference<S: Into<String>>(r: S) -> Self {
Self::Reference(r.into())
}
pub(crate) fn text<S: Into<String>>(t: S) -> Self {
Self::Text(t.into())
}
pub(crate) fn var<S: Into<String>>(name: S, value: VariableValue) -> Self {
Self::Variable {
name: name.into(),
value,
}
}
}