flowcode-core 0.4.3-alpha

Core execution engine for FlowCode data scripting language
Documentation
// flowcode-core/src/ast.rs
// Central typed AST definitions shared across parser, executor and commands.
// Currently only `ArgValue` is defined. More data-structures (e.g. full
// expression trees) can be added here in the future.

#![allow(clippy::upper_case_acronyms)]

#[cfg(feature = "typed-args")]
use chrono::NaiveDate;
use crate::types::TypedValue;

/// A single CLI argument or value that has been type-classified by the parser.
#[derive(Debug, Clone, PartialEq)]
pub enum ArgValue {
    /// Tabular data, typically from CSV or database results.
    Table(Vec<Vec<TypedValue>>), // rows ⟶ columns already typed
    /// Numerical value, either integer or floating-point.
    Number(f64),
    /// Textual data.
    String(String),
    /// Boolean value.
    Bool(bool),
    /// Absence of a value.
    Null,
    /// ISO-8601 calendar date `YYYY-MM-DD` (only when the `typed-args` feature
    /// is enabled; otherwise this variant is absent to avoid pulling chrono).
    #[cfg(feature = "typed-args")]
    Date(NaiveDate),
}

impl core::fmt::Display for ArgValue {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ArgValue::Table(_) => write!(f, "[Table]"),
            ArgValue::Number(n) => write!(f, "{}", n),
            ArgValue::String(s) => write!(f, "{}", s),
            ArgValue::Bool(b) => write!(f, "{}", b),
            ArgValue::Null => write!(f, "null"),
            #[cfg(feature = "typed-args")]
            ArgValue::Date(d) => write!(f, "{}", d),
        }
    }
}

impl ArgValue {
    /// Attempt to extract a numeric value. Returns `None` for non-numeric variants.
    pub fn as_number(&self) -> Option<f64> {
        match self {
            ArgValue::Number(n) => Some(*n),
            _ => None,
        }
    }
}