flowcode_core/ast.rs
1// flowcode-core/src/ast.rs
2// Central typed AST definitions shared across parser, executor and commands.
3// Currently only `ArgValue` is defined. More data-structures (e.g. full
4// expression trees) can be added here in the future.
5
6#![allow(clippy::upper_case_acronyms)]
7
8#[cfg(feature = "typed-args")]
9use chrono::NaiveDate;
10use crate::types::TypedValue;
11
12/// A single CLI argument or value that has been type-classified by the parser.
13#[derive(Debug, Clone, PartialEq)]
14pub enum ArgValue {
15 /// Tabular data, typically from CSV or database results.
16 Table(Vec<Vec<TypedValue>>), // rows ⟶ columns already typed
17 /// Numerical value, either integer or floating-point.
18 Number(f64),
19 /// Textual data.
20 String(String),
21 /// Boolean value.
22 Bool(bool),
23 /// Absence of a value.
24 Null,
25 /// ISO-8601 calendar date `YYYY-MM-DD` (only when the `typed-args` feature
26 /// is enabled; otherwise this variant is absent to avoid pulling chrono).
27 #[cfg(feature = "typed-args")]
28 Date(NaiveDate),
29}
30
31impl core::fmt::Display for ArgValue {
32 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33 match self {
34 ArgValue::Table(_) => write!(f, "[Table]"),
35 ArgValue::Number(n) => write!(f, "{}", n),
36 ArgValue::String(s) => write!(f, "{}", s),
37 ArgValue::Bool(b) => write!(f, "{}", b),
38 ArgValue::Null => write!(f, "null"),
39 #[cfg(feature = "typed-args")]
40 ArgValue::Date(d) => write!(f, "{}", d),
41 }
42 }
43}
44
45impl ArgValue {
46 /// Attempt to extract a numeric value. Returns `None` for non-numeric variants.
47 pub fn as_number(&self) -> Option<f64> {
48 match self {
49 ArgValue::Number(n) => Some(*n),
50 _ => None,
51 }
52 }
53}