flowcode-core 0.4.3-alpha

Core execution engine for FlowCode data scripting language
Documentation
// Combine command implementation
#[allow(dead_code)] // This function is called dynamically by the executor
use crate::ast::ArgValue;
use crate::FCError;

/// Combine over a list of typed args. Currently supports numeric inputs. Any
/// non-numeric argument triggers an `E203` higher up in executor.
pub fn combine(args: &[ArgValue]) -> Result<f64, FCError> {
    if args.is_empty() {
        return Err(FCError::InsufficientData("combine requires at least one argument".to_string()));
    }
    let sum: f64 = args.iter().map(|arg| match arg {
        ArgValue::Number(n) => *n,
        _ => 0.0, // This should not happen if type checking is done before
    }).sum();
    Ok(sum)
}

// New error handling code
pub fn combine_with_error_handling(args: &[ArgValue]) -> Result<f64, FCError> {
    if args.len() < 3 {
        return Err(FCError::InsufficientData("Expected at least 3 arguments: source1, source2, and on clause".to_string()));
    }

    let source1 = match args.get(0) {
        Some(ArgValue::String(s)) => s.clone(),
        Some(other) => return Err(FCError::InvalidArgument(format!("Expected String for source1, got {:?}", other))),
        None => return Err(FCError::InsufficientData("Missing source1 data".to_string())),
    };

    let source2 = match args.get(1) {
        Some(ArgValue::String(s)) => s.clone(),
        Some(other) => return Err(FCError::InvalidArgument(format!("Expected String for source2, got {:?}", other))),
        None => return Err(FCError::InsufficientData("Missing source2 data".to_string())),
    };

    let on_clause = match args.get(2) {
        Some(ArgValue::String(s)) => s.clone(),
        Some(other) => return Err(FCError::InvalidArgument(format!("Expected string for on clause, got {:?}", other))),
        None => return Err(FCError::InsufficientData("Missing on clause for combination".to_string())),
    };

    // Placeholder for actual combination logic
    let combined_data = format!("Combined data from {} and {} on {}", source1, source2, on_clause);
    println!("{}", combined_data); // Using the variable to avoid warning
    Ok(0.0) // Return a dummy value for now
}