spaj_toggle/
spaj_toggle.rs

1use std::fs;
2use std::path::Path;
3use json_eval_rs::JSONEval;
4use serde_json::json;
5
6fn main() {
7    println!("\nšŸš€ JSON Evaluation - SPAJ Toggle Example\n");
8
9    let schema_path = Path::new("samples/spaj.json");
10    let schema_str = fs::read_to_string(schema_path).expect("Failed to read schema");
11
12    // Initial data with minimal context required
13    let context_str = json!({
14        "agentProfile": { "sob": "AG" } 
15    }).to_string();
16
17    let initial_data = json!({
18        "illustration": {
19            "basicinformation": {
20                "print_polflag": false
21            }
22        }
23    }).to_string();
24
25    // Initialize logic
26    let mut eval = JSONEval::new(&schema_str, Some(&context_str), Some(&initial_data))
27        .expect("Failed to create JSONEval");
28
29    // Helper to check visibility
30    let check_visibility = |eval: &mut JSONEval, expected_hidden: bool, step: &str| {
31        let result = eval.get_evaluated_schema(false);
32        let hidden = result.pointer("/illustration/properties/basicinformation/properties/print_poladdress/condition/hidden")
33            .and_then(|v| v.as_bool());
34        
35        match hidden {
36            Some(val) => {
37                if val == expected_hidden {
38                    println!("āœ… {}: Hidden = {} (Expected: {})", step, val, expected_hidden);
39                } else {
40                    println!("āŒ {}: Hidden = {} (Expected: {})", step, val, expected_hidden);
41                }
42            },
43            None => println!("āŒ {}: 'hidden' property not found", step),
44        }
45    };
46
47    // Step 1: Initial state (false)
48    println!("Step 1: Initial State (print_polflag: false)");
49    eval.evaluate(&initial_data, Some(&context_str), None).expect("Evaluation failed");
50    check_visibility(&mut eval, true, "Initial check");
51
52    // Step 2: Toggle to true
53    println!("\nStep 2: Toggle True (print_polflag: true)");
54    let data_true = json!({
55        "illustration": {
56            "basicinformation": {
57                "print_polflag": true
58            }
59        }
60    }).to_string();
61    eval.evaluate(&data_true, Some(&context_str), None).expect("Evaluation failed");
62    check_visibility(&mut eval, false, "Toggle ON check");
63
64    // Step 3: Toggle back to false
65    println!("\nStep 3: Toggle False (print_polflag: false)");
66    let data_false = json!({
67        "illustration": {
68            "basicinformation": {
69                "print_polflag": false
70            }
71        }
72    }).to_string();
73    eval.evaluate(&data_false, Some(&context_str), None).expect("Evaluation failed");
74    
75    let hidden_path = "#/illustration/properties/basicinformation/properties/print_poladdress/condition/hidden";
76    if let Some(deps) = eval.dependencies.get(hidden_path) {
77        println!("Debug: Dependencies for hidden: {:?}", deps);
78    } else {
79        println!("Debug: No dependencies found for hidden path");
80    }
81
82    // Debug: Print current flag value
83    if let Some(val) = eval.get_evaluated_schema(false).pointer("/illustration/properties/basicinformation/properties/print_polflag/value") {
84         println!("Debug: print_polflag value is: {}", val);
85    }
86
87    check_visibility(&mut eval, true, "Toggle OFF check");
88}