custom_simple/
custom_simple.rs

1// Simple example demonstrating custom operators with simplified API
2
3use datalogic_rs::value::NumberValue;
4use datalogic_rs::{DataLogic, DataValue};
5use std::error::Error;
6
7// Simple custom operator that doubles a number
8fn double(args: Vec<DataValue>) -> std::result::Result<DataValue, String> {
9    if args.is_empty() {
10        return Err("double operator requires at least one argument".to_string());
11    }
12
13    if let Some(n) = args[0].as_f64() {
14        return Ok(DataValue::Number(NumberValue::from_f64(n * 2.0)));
15    }
16
17    Err("Argument must be a number".to_string())
18}
19
20// String operator example - converts a string to uppercase
21fn to_uppercase(args: Vec<DataValue>) -> std::result::Result<DataValue, String> {
22    if args.is_empty() {
23        return Err("to_uppercase requires a string argument".to_string());
24    }
25
26    if let Some(s) = args[0].as_str() {
27        // Use Box::leak to create a static string
28        let upper = s.to_uppercase();
29        let upper_str = Box::leak(upper.into_boxed_str());
30        return Ok(DataValue::String(upper_str));
31    }
32
33    Err("Argument must be a string".to_string())
34}
35
36// Boolean operator example - checks if a number is even
37fn is_even(args: Vec<DataValue>) -> std::result::Result<DataValue, String> {
38    if args.is_empty() {
39        return Err("is_even requires a number argument".to_string());
40    }
41
42    if let Some(n) = args[0].as_i64() {
43        return Ok(DataValue::Bool(n % 2 == 0));
44    }
45
46    Err("Argument must be a number".to_string())
47}
48
49fn main() -> std::result::Result<(), Box<dyn Error>> {
50    // Create a DataLogic instance
51    let mut dl = DataLogic::new();
52
53    // Register our simple custom operators
54    dl.register_simple_operator("double", double);
55    dl.register_simple_operator("to_uppercase", to_uppercase);
56    dl.register_simple_operator("is_even", is_even);
57
58    // Example 1: Double a number
59    let result = dl.evaluate_str(r#"{"double": 5}"#, r#"{}"#, None)?;
60    println!("Double 5 = {}", result); // Should print: Double 5 = 10
61
62    // Example 2: Use the custom operator with a variable
63    let result = dl.evaluate_str(r#"{"double": {"var": "value"}}"#, r#"{"value": 7.5}"#, None)?;
64    println!("Double 7.5 = {}", result); // Should print: Double 7.5 = 15
65
66    // Example 3: Convert a string to uppercase
67    let result = dl.evaluate_str(r#"{"to_uppercase": "hello world"}"#, r#"{}"#, None)?;
68    println!("Uppercase 'hello world' = {}", result); // Should print: HELLO WORLD
69
70    // Example 4: Check if a number is even
71    let result = dl.evaluate_str(r#"{"is_even": 42}"#, r#"{}"#, None)?;
72    println!("Is 42 even? {}", result); // Should print: true
73
74    // Example 5: Check if a number is even with a variable
75    let result = dl.evaluate_str(
76        r#"{"is_even": {"var": "number"}}"#,
77        r#"{"number": 7}"#,
78        None,
79    )?;
80    println!("Is 7 even? {}", result); // Should print: false
81
82    Ok(())
83}