stuff around publishing crate successfuly done
This commit is contained in:
73
steel_decimal/examples/with_variables.rs
Normal file
73
steel_decimal/examples/with_variables.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
// examples/with_variables.rs
|
||||
use steel_decimal::SteelDecimal;
|
||||
use steel::steel_vm::engine::Engine;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
// Create variables
|
||||
let mut variables = HashMap::new();
|
||||
variables.insert("price".to_string(), "29.99".to_string());
|
||||
variables.insert("quantity".to_string(), "5".to_string());
|
||||
variables.insert("tax_rate".to_string(), "0.08".to_string());
|
||||
|
||||
// Create Steel Decimal with variables
|
||||
let steel_decimal = SteelDecimal::with_variables(variables);
|
||||
|
||||
// Script using variables
|
||||
let script = "(+ (* $price $quantity) (* (* $price $quantity) $tax_rate))";
|
||||
let transformed = steel_decimal.transform(script);
|
||||
|
||||
println!("Original script: {}", script);
|
||||
println!("Transformed script: {}", transformed);
|
||||
|
||||
// Create VM and register functions
|
||||
let mut vm = Engine::new();
|
||||
steel_decimal.register_functions(&mut vm);
|
||||
|
||||
// Execute the script
|
||||
match vm.compile_and_run_raw_program(transformed) {
|
||||
Ok(results) => {
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Total with tax: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Demonstrate adding variables dynamically
|
||||
let mut steel_decimal = SteelDecimal::new();
|
||||
steel_decimal.add_variable("x".to_string(), "10.5".to_string());
|
||||
steel_decimal.add_variable("y".to_string(), "20.3".to_string());
|
||||
|
||||
let simple_script = "(+ $x $y)";
|
||||
println!("\nSimple script: {}", simple_script);
|
||||
|
||||
match steel_decimal.parse_and_execute(simple_script) {
|
||||
Ok(results) => {
|
||||
if let Some(last_result) = results.last() {
|
||||
println!("Simple result: {:?}", last_result);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Show variable validation
|
||||
println!("\nVariable validation:");
|
||||
match steel_decimal.validate_script("(+ $x $y)") {
|
||||
Ok(()) => println!("Script is valid"),
|
||||
Err(e) => println!("Script error: {}", e),
|
||||
}
|
||||
|
||||
match steel_decimal.validate_script("(+ $x $undefined_var)") {
|
||||
Ok(()) => println!("Script is valid"),
|
||||
Err(e) => println!("Script error: {}", e),
|
||||
}
|
||||
|
||||
// Extract dependencies
|
||||
let dependencies = steel_decimal.extract_dependencies("(+ $x $y $z)");
|
||||
println!("Dependencies: {:?}", dependencies);
|
||||
}
|
||||
Reference in New Issue
Block a user