use super::{Ast, FunctionArgs, FunctionName, VariableName};
use crate::Expand;
use a1_notation::{Address, Column, Row};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, PartialEq, 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,
},
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum VariableValue {
Absolute(Address),
Ast(Ast),
ColumnRelative { column: Column, scope: Expand },
Row(Row),
RowRelative { row: Row, scope: Expand },
}
impl Node {
#[cfg(test)]
pub(crate) fn fn_def<S: Into<String>>(name: S, args: &[&str], body: Self) -> Self {
Self::Function {
name: name.into(),
args: args.iter().map(|a| a.to_string()).collect(),
body: Box::new(body),
}
}
pub(crate) fn fn_call<S: Into<String>>(name: S, args: &[Self]) -> Self {
Self::FunctionCall {
name: name.into(),
args: args.iter().map(|a| Box::new(a.to_owned())).collect(),
}
}
pub(crate) fn infix_fn_call<S: Into<String>>(left: Self, operator: S, right: Self) -> Self {
Self::InfixFunctionCall {
left: Box::new(left),
operator: operator.into(),
right: Box::new(right),
}
}
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,
}
}
}