flowcode_core/commands/
combine.rs

1// Combine command implementation
2#[allow(dead_code)] // This function is called dynamically by the executor
3use crate::ast::ArgValue;
4use crate::FCError;
5
6/// Combine over a list of typed args. Currently supports numeric inputs. Any
7/// non-numeric argument triggers an `E203` higher up in executor.
8pub fn combine(args: &[ArgValue]) -> Result<f64, FCError> {
9    if args.is_empty() {
10        return Err(FCError::InsufficientData("combine requires at least one argument".to_string()));
11    }
12    let sum: f64 = args.iter().map(|arg| match arg {
13        ArgValue::Number(n) => *n,
14        _ => 0.0, // This should not happen if type checking is done before
15    }).sum();
16    Ok(sum)
17}
18
19// New error handling code
20pub fn combine_with_error_handling(args: &[ArgValue]) -> Result<f64, FCError> {
21    if args.len() < 3 {
22        return Err(FCError::InsufficientData("Expected at least 3 arguments: source1, source2, and on clause".to_string()));
23    }
24
25    let source1 = match args.get(0) {
26        Some(ArgValue::String(s)) => s.clone(),
27        Some(other) => return Err(FCError::InvalidArgument(format!("Expected String for source1, got {:?}", other))),
28        None => return Err(FCError::InsufficientData("Missing source1 data".to_string())),
29    };
30
31    let source2 = match args.get(1) {
32        Some(ArgValue::String(s)) => s.clone(),
33        Some(other) => return Err(FCError::InvalidArgument(format!("Expected String for source2, got {:?}", other))),
34        None => return Err(FCError::InsufficientData("Missing source2 data".to_string())),
35    };
36
37    let on_clause = match args.get(2) {
38        Some(ArgValue::String(s)) => s.clone(),
39        Some(other) => return Err(FCError::InvalidArgument(format!("Expected string for on clause, got {:?}", other))),
40        None => return Err(FCError::InsufficientData("Missing on clause for combination".to_string())),
41    };
42
43    // Placeholder for actual combination logic
44    let combined_data = format!("Combined data from {} and {} on {}", source1, source2, on_clause);
45    println!("{}", combined_data); // Using the variable to avoid warning
46    Ok(0.0) // Return a dummy value for now
47}