| Crates.io | clia-config-expr |
| lib.rs | clia-config-expr |
| version | 0.1.6 |
| created_at | 2025-07-04 01:19:05.227405+00 |
| updated_at | 2025-11-04 08:46:38.021182+00 |
| description | A flexible configuration expression evaluator with JSON schema support |
| homepage | |
| repository | https://github.com/clia/config-expr |
| max_upload_size | |
| id | 1737338 |
| size | 50,434 |
A flexible configuration expression evaluator that supports rule systems defined by JSON schema.
| Operator | Description | Example | Note |
|---|---|---|---|
equals |
Exact equals | "platform" equals "RTD" |
String comparison |
contains |
Contains | "platform" contains "RTD" |
String comparison |
prefix |
Prefix match | "platform" prefix "Hi" |
String comparison |
suffix |
Suffix match | "platform" suffix "Pro" |
String comparison |
regex |
Regex match | "version" regex "^v\\d+\\.\\d+\\.\\d+$" |
String comparison |
gt |
Greater than | "score" gt "80" |
Numeric comparison |
lt |
Less than | "age" lt "18" |
Numeric comparison |
ge |
Greater than or equal | "level" ge "5" |
Numeric comparison |
le |
Less than or equal | "temperature" le "25.5" |
Numeric comparison |
[dependencies]
clia-config-expr = "0.1.1"
use clia_config_expr::{evaluate_json, validate_json};
use std::collections::HashMap;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define rules
let rules = r#"
{
"rules": [
{
"if": {
"and": [
{ "field": "platform", "op": "contains", "value": "RTD" },
{ "field": "region", "op": "equals", "value": "CN" }
]
},
"then": "chip_rtd_cn"
},
{
"if": {
"field": "platform",
"op": "prefix",
"value": "Hi"
},
"then": "chip_hi"
},
{
"if": {
"field": "score",
"op": "ge",
"value": "80"
},
"then": "high_score"
}
],
"fallback": "default_chip"
}
"#;
// Validate rules
validate_json(rules)?;
// Prepare parameters
let mut params = HashMap::new();
params.insert("platform".to_string(), "RTD-2000".to_string());
params.insert("region".to_string(), "CN".to_string());
// Evaluate rules
let result = evaluate_json(rules, ¶ms)?;
println!("Result: {:?}", result); // Some(String("chip_rtd_cn"))
Ok(())
}
{
"rules": [
{
"if": "<condition expression>",
"then": "<return value>"
}
],
"fallback": "<optional default return value>"
}
String comparison:
{
"field": "platform",
"op": "equals",
"value": "RTD"
}
Numeric comparison:
{
"field": "score",
"op": "ge",
"value": "80"
}
{
"and": [
{ "field": "platform", "op": "contains", "value": "RTD" },
{ "field": "region", "op": "equals", "value": "CN" }
]
}
{
"or": [
{ "field": "platform", "op": "equals", "value": "MT9950" },
{ "field": "platform", "op": "equals", "value": "MT9638" }
]
}
{
"if": { "field": "platform", "op": "equals", "value": "RTD" },
"then": "chip_rtd"
}
{
"if": { "field": "platform", "op": "equals", "value": "RTD" },
"then": {
"chip": "rtd",
"config": {
"memory": "2GB",
"cpu": "ARM"
}
}
}
ConfigEvaluator: Configuration expression evaluatorConfigRules: Rule set definitionCondition: Condition expressionRuleResult: Rule result (string or JSON object)Operator: Operator enumerationevaluate_json(json, params): Directly evaluate from JSON stringvalidate_json(json): Validate if JSON rules are validConfigEvaluator::from_json(json): Create evaluator from JSONevaluator.evaluate(params): Evaluate parameters and return resultcargo run --example basic_usage
cargo test
This project is dual-licensed under MIT or Apache-2.0.
A JSON-based configuration expression processor.