Skip to main content

spaj_toggle/
spaj_toggle.rs

1use json_eval_rs::JSONEval;
2use serde_json::json;
3use std::fs;
4use std::path::Path;
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    })
16    .to_string();
17
18    let initial_data = json!({
19        "illustration": {
20            "basicinformation": {
21                "print_polflag": false
22            }
23        }
24    })
25    .to_string();
26
27    // Initialize logic
28    let mut eval = JSONEval::new(&schema_str, Some(&context_str), Some(&initial_data))
29        .expect("Failed to create JSONEval");
30
31    // Helper to check visibility
32    let check_visibility = |eval: &mut JSONEval, expected_hidden: bool, step: &str| {
33        let result = eval.get_evaluated_schema(false);
34        let hidden = result.pointer("/illustration/properties/basicinformation/properties/print_poladdress/condition/hidden")
35            .and_then(|v| v.as_bool());
36
37        match hidden {
38            Some(val) => {
39                if val == expected_hidden {
40                    println!(
41                        "āœ… {}: Hidden = {} (Expected: {})",
42                        step, val, expected_hidden
43                    );
44                } else {
45                    println!(
46                        "āŒ {}: Hidden = {} (Expected: {})",
47                        step, val, expected_hidden
48                    );
49                }
50            }
51            None => println!("āŒ {}: 'hidden' property not found", step),
52        }
53    };
54
55    // Step 1: Initial state (false)
56    println!("Step 1: Initial State (print_polflag: false)");
57    eval.evaluate(&initial_data, Some(&context_str), None, None)
58        .expect("Evaluation failed");
59    check_visibility(&mut eval, true, "Initial check");
60
61    // Step 2: Toggle to true
62    println!("\nStep 2: Toggle True (print_polflag: true)");
63    let data_true = json!({
64        "illustration": {
65            "basicinformation": {
66                "print_polflag": true
67            }
68        }
69    })
70    .to_string();
71    eval.evaluate(&data_true, Some(&context_str), None, None)
72        .expect("Evaluation failed");
73    check_visibility(&mut eval, false, "Toggle ON check");
74
75    // Step 3: Toggle back to false
76    println!("\nStep 3: Toggle False (print_polflag: false)");
77    let data_false = json!({
78        "illustration": {
79            "basicinformation": {
80                "print_polflag": false
81            }
82        }
83    })
84    .to_string();
85    eval.evaluate(&data_false, Some(&context_str), None, None)
86        .expect("Evaluation failed");
87
88    let hidden_path =
89        "#/illustration/properties/basicinformation/properties/print_poladdress/condition/hidden";
90    if let Some(deps) = eval.dependencies.get(hidden_path) {
91        println!("Debug: Dependencies for hidden: {:?}", deps);
92    } else {
93        println!("Debug: No dependencies found for hidden path");
94    }
95
96    // Debug: Print current flag value
97    if let Some(val) = eval
98        .get_evaluated_schema(false)
99        .pointer("/illustration/properties/basicinformation/properties/print_polflag/value")
100    {
101        println!("Debug: print_polflag value is: {}", val);
102    }
103
104    check_visibility(&mut eval, true, "Toggle OFF check");
105}