clia-config-expr

Crates.ioclia-config-expr
lib.rsclia-config-expr
version0.1.6
created_at2025-07-04 01:19:05.227405+00
updated_at2025-11-04 08:46:38.021182+00
descriptionA flexible configuration expression evaluator with JSON schema support
homepage
repositoryhttps://github.com/clia/config-expr
max_upload_size
id1737338
size50,434
clia (clia)

documentation

README

Config Expression

δΈ­ζ–‡η‰ˆ

A flexible configuration expression evaluator that supports rule systems defined by JSON schema.

Features

  • πŸš€ High Performance: Implemented in Rust with zero-copy parsing
  • πŸ“ JSON Schema: Clear and semantically explicit JSON rule definitions
  • πŸ”§ Extensible: Supports multiple operators and compound conditions
  • 🎯 Type Safe: Full Rust type system support
  • βœ… Validator: Built-in rule validity validation
  • 🌐 Frontend/Backend Friendly: Easy to configure on frontend and execute on backend

Supported Operators

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

Supported Condition Types

  • Simple Condition: Single field comparison
  • AND Condition: All sub-conditions must be satisfied
  • OR Condition: At least one sub-condition must be satisfied
  • Nested Conditions: Supports arbitrary levels of condition nesting

Getting Started

Add Dependency

[dependencies]
clia-config-expr = "0.1.1"

Basic Usage

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, &params)?;
    println!("Result: {:?}", result); // Some(String("chip_rtd_cn"))

    Ok(())
}

JSON Schema Structure

Basic Structure

{
    "rules": [
        {
            "if": "<condition expression>",
            "then": "<return value>"
        }
    ],
    "fallback": "<optional default return value>"
}

Condition Expressions

Simple Condition

String comparison:

{
    "field": "platform",
    "op": "equals",
    "value": "RTD"
}

Numeric comparison:

{
    "field": "score",
    "op": "ge",
    "value": "80"
}

AND Condition

{
    "and": [
        { "field": "platform", "op": "contains", "value": "RTD" },
        { "field": "region", "op": "equals", "value": "CN" }
    ]
}

OR Condition

{
    "or": [
        { "field": "platform", "op": "equals", "value": "MT9950" },
        { "field": "platform", "op": "equals", "value": "MT9638" }
    ]
}

Return Value Types

String Return Value

{
    "if": { "field": "platform", "op": "equals", "value": "RTD" },
    "then": "chip_rtd"
}

JSON Object Return Value

{
    "if": { "field": "platform", "op": "equals", "value": "RTD" },
    "then": {
        "chip": "rtd",
        "config": {
            "memory": "2GB",
            "cpu": "ARM"
        }
    }
}

API Documentation

Main Types

  • ConfigEvaluator: Configuration expression evaluator
  • ConfigRules: Rule set definition
  • Condition: Condition expression
  • RuleResult: Rule result (string or JSON object)
  • Operator: Operator enumeration

Main Methods

  • evaluate_json(json, params): Directly evaluate from JSON string
  • validate_json(json): Validate if JSON rules are valid
  • ConfigEvaluator::from_json(json): Create evaluator from JSON
  • evaluator.evaluate(params): Evaluate parameters and return result

Run Examples

cargo run --example basic_usage

Run Tests

cargo test

License

This project is dual-licensed under MIT or Apache-2.0.

A JSON-based configuration expression processor.
Commit count: 0

cargo fmt