Function evaluate_json

Source
pub fn evaluate_json(
    json: &str,
    params: &HashMap<String, String>,
) -> Result<Option<RuleResult>, ConfigExprError>
Expand description

便利方法:直接从JSON字符串评估

Examples found in repository?
examples/basic_usage.rs (line 64)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // 示例1: 基本用法 - 直接从JSON字符串评估
6    println!("=== 示例1: 基本用法 ===");
7
8    let json_rules = r#"
9    {
10        "rules": [
11            {
12                "if": {
13                    "and": [
14                        { "field": "platform", "op": "contains", "value": "RTD" },
15                        { "field": "region", "op": "equals", "value": "CN" }
16                    ]
17                },
18                "then": "chip_rtd_cn"
19            },
20            {
21                "if": {
22                    "or": [
23                        { "field": "platform", "op": "equals", "value": "MT9950" },
24                        { "field": "platform", "op": "equals", "value": "MT9638" }
25                    ]
26                },
27                "then": "chip_mt"
28            },
29            {
30                "if": {
31                    "field": "platform",
32                    "op": "prefix",
33                    "value": "Hi"
34                },
35                "then": "chip_hi"
36            }
37        ],
38        "fallback": "default_chip"
39    }
40    "#;
41
42    // 验证规则是否合法
43    println!("验证规则...");
44    validate_json(json_rules)?;
45    println!("✓ 规则验证通过");
46
47    // 测试不同的参数组合
48    let test_cases = vec![
49        (
50            vec![("platform", "RTD-2000"), ("region", "CN")],
51            "应该匹配 chip_rtd_cn",
52        ),
53        (vec![("platform", "MT9950")], "应该匹配 chip_mt"),
54        (vec![("platform", "Hi3516")], "应该匹配 chip_hi"),
55        (vec![("platform", "Unknown")], "应该使用 fallback"),
56    ];
57
58    for (params_vec, description) in test_cases {
59        let mut params = HashMap::new();
60        for (key, value) in params_vec {
61            params.insert(key.to_string(), value.to_string());
62        }
63
64        let result = evaluate_json(json_rules, &params)?;
65        println!("测试: {} -> {:?}", description, result);
66    }
67
68    // 示例2: JSON对象结果
69    println!("\n=== 示例2: JSON对象结果 ===");
70
71    let json_with_object = r#"
72    {
73        "rules": [
74            {
75                "if": {
76                    "field": "platform",
77                    "op": "equals",
78                    "value": "RTD"
79                },
80                "then": {
81                    "chip": "rtd",
82                    "config": {
83                        "memory": "2GB",
84                        "cpu": "ARM",
85                        "features": ["wifi", "bluetooth"]
86                    }
87                }
88            }
89        ]
90    }
91    "#;
92
93    let mut params = HashMap::new();
94    params.insert("platform".to_string(), "RTD".to_string());
95
96    let result = evaluate_json(json_with_object, &params)?;
97    if let Some(RuleResult::Object(obj)) = result {
98        println!("匹配到JSON对象结果:");
99        println!("  chip: {}", obj["chip"]);
100        println!("  memory: {}", obj["config"]["memory"]);
101        println!("  cpu: {}", obj["config"]["cpu"]);
102        println!("  features: {:?}", obj["config"]["features"]);
103    }
104
105    // 示例3: 使用ConfigEvaluator结构体
106    println!("\n=== 示例3: 使用ConfigEvaluator ===");
107
108    let evaluator = ConfigEvaluator::from_json(json_rules)?;
109
110    let mut params = HashMap::new();
111    params.insert("platform".to_string(), "Hi3516DV300".to_string());
112
113    let result = evaluator.evaluate(&params);
114    println!("Hi3516DV300 -> {:?}", result);
115
116    // 示例4: 正则表达式匹配
117    println!("\n=== 示例4: 正则表达式匹配 ===");
118
119    let regex_rules = r#"
120    {
121        "rules": [
122            {
123                "if": {
124                    "field": "version",
125                    "op": "regex",
126                    "value": "^v\\d+\\.\\d+\\.\\d+$"
127                },
128                "then": "valid_version"
129            }
130        ],
131        "fallback": "invalid_version"
132    }
133    "#;
134
135    let test_versions = vec!["v1.2.3", "v10.0.1", "1.2.3", "v1.2"];
136
137    for version in test_versions {
138        let mut params = HashMap::new();
139        params.insert("version".to_string(), version.to_string());
140
141        let result = evaluate_json(regex_rules, &params)?;
142        println!("版本 {} -> {:?}", version, result);
143    }
144
145    // 示例5: 复杂嵌套条件
146    println!("\n=== 示例5: 复杂嵌套条件 ===");
147
148    let complex_rules = r#"
149    {
150        "rules": [
151            {
152                "if": {
153                    "and": [
154                        {
155                            "or": [
156                                { "field": "platform", "op": "prefix", "value": "Hi" },
157                                { "field": "platform", "op": "prefix", "value": "MT" }
158                            ]
159                        },
160                        { "field": "region", "op": "equals", "value": "CN" },
161                        { "field": "env", "op": "contains", "value": "prod" }
162                    ]
163                },
164                "then": {
165                    "chip_type": "cn_production",
166                    "optimization": "high"
167                }
168            }
169        ]
170    }
171    "#;
172
173    let mut params = HashMap::new();
174    params.insert("platform".to_string(), "Hi3516".to_string());
175    params.insert("region".to_string(), "CN".to_string());
176    params.insert("env".to_string(), "production".to_string());
177
178    let result = evaluate_json(complex_rules, &params)?;
179    println!("复杂条件匹配结果: {:?}", result);
180
181    Ok(())
182}