Operators Reference
DataLogic-rs provides a comprehensive set of operators for building powerful expressions. These operators are organized into categories based on their functionality.
Arithmetic Operators
Perform mathematical calculations: +, -, *, /, %, min, max
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulo
- min Minimum
- max Maximum
Comparison Operators
Compare values: ==, ===, !=, !==, >, >=, <, <=
- == Equal
- === Strict Equal
- != Not Equal
- !== Strict Not Equal
- > Greater Than
- < Less Than
- >= Greater Than or Equal
- <= Less Than or Equal
Array Operators
Manipulate arrays: map, filter, reduce, some, all, merge
- map Transform each item
- filter Select matching items
- reduce Accumulate values
- all Check if all items match
- some Check if any item matches
- merge Combine arrays
- in Check if value exists in array
String Operators
Manipulate strings: cat, substr, in, trim, length
- cat Concatenate strings
- substr Extract substring
Variable Operators
Access data: var, missing, missing_some
- var Access variables from data
- missing Check for missing fields
- missing_some Check if some required fields exist
- valAccess values from data
- exists Check if a value exists
Control Flow Operators
Control logic: if, and, or, !, ??
- if Conditional execution
- and Logical AND
- or Logical OR
- ! Logical NOT
- ?? Null coalescing
Date & Time Operators
Work with dates: now, date_diff
- now Current timestamp
- date_diff Calculate difference between dates
Error Handling Operators
Handle errors: try, throw
- try Try an operation with fallback
- throw Throw an error
Coalescing Operator
Handle null/undefined values with fallbacks
- ?? Nullish coalescing operator
Custom Operators
DataLogic-rs allows you to extend the engine with custom operators. This provides flexibility to implement domain-specific functionality beyond the standard operators.
use datalogic_rs::{DataLogic, Value};
// Create a new instance
let mut dl = DataLogic::new();
// Add a custom operator that doubles a number
dl.add_operation("double", |args, data, _ops| {
if args.is_empty() {
return Value::Null;
}
// Evaluate the first argument
let value = dl.apply(&args[0], data, None);
// Check if it's a number and double it
if let Value::Number(n) = value {
Value::Number(n * 2.0)
} else {
Value::Null
}
});