首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【大家的项目】部分移植nodejs的json-rules-engine到rust

【大家的项目】部分移植nodejs的json-rules-engine到rust

作者头像
MikeLoveRust
发布2020-11-23 10:27:29
发布2020-11-23 10:27:29
1.3K00
代码可运行
举报
运行总次数:0
代码可运行

json-rules-engine

最近有一个老旧的nodejs项目需要移植到rust,其中一部分严重依赖nodejs的json-rules-engine库,于是昨天抽时间移植了该项目的部分功能到rust,尚不支持优先级和缓存。

项目地址

https://github.com/GopherJ/json-rules-engine-rs

安装

代码语言:javascript
代码运行次数:0
运行
复制
[dependencies]
json-rules-engine = { version = "0.7.0", features = ["email"] }
tokio = { version = "0.3.3", features = ["macros"] }
serde_json = { version = "*" }
anyhow = { version = "*" }

特性

  • 很多的类型安全的内置操作符如, string_equals, int_in_range...等
  • 支持All, Any, AtLeast操作符
  • 支持匹配成功后的多个对应事件如HTTP post到callback_url,发送邮件,适合报警
  • json序列化反序列化
  • 内置moustache支持
  • 自定义脚本
  • 自定义函数

实例

代码语言:javascript
代码运行次数:0
运行
复制
use json_rules_engine::{Engine, Rule, Map, from_dynamic};
use serde_json::json;
use serde::{Serialize, Deserialize};

#[derive(Deserialize, Serialize)]
struct Facts {
    name: String,
    age: u8,
    action: String
}

fn age_greater_than20_less_than_inclusive25(p: Map) -> bool {
    let facts: Facts = from_dynamic(&p.into()).unwrap();
    facts.age > 20 && facts.age <= 25
}

#[tokio::main]
async main() -> anyhow::Result<()> {
    let sendgrid_api_key = "kjsldkjslkjlwkjkjew";

    let rule_json = json!({
        "conditions": {
            "and": [
                {
                    "field": "name",
                    "operator": "string_equals",
                    "value": "Cheng JIANG"
                },
                {
                    "field": "age",
                    "operator": "int_in_range",
                    "value": [20, 25] 
                },
                {
                    "script": "facts.age > 20 && facts.age <= 25",
                },
                {
                    "script": "my_function(facts)",
                },
                {
                    "field": "action",
                    "operator": "string_equals",
                    "value": "coding in rust"
                }
            ]
        },
        "events": [
            {
                "type": "post_to_callback_url",
                "params": {
                    "callback_url": "http://example.com/people/conding_in_rust",
                    "type": "info",
                    "title": "Another person is coding in rust",
                    "message": "Name: {{ name }}, Age: {{ age }}, Action: {{ action }},"
                }
            },
            {
                "type": "email_notification",
                "params": {
                    "from": "alex_cj96@foxmail.com",
                    "to": ["abc.def@gmail.com"],
                    "type": "info",
                    "title": "Another person is coding in rust",
                    "message": "Name: {{ name }}, Age: {{ age }}, Action: {{ action }},"
                }
            }
        ]
    });

    let rule: Rule = serde_json::from_str::<Rule>(&serde_json::to_string(&rule_json).unwrap()).unwrap();

    let mut engine = Engine::new(sendgrid_api_key);
    engine.add_rule(rule);
    engine.add_function("my_function", age_greater_than20_less_than_inclusive25);

    let facts = json!({
        "name": "Cheng JIANG",
        "age": 24,
        "action": "coding in rust",
    });

    let rule_results = engine.run(&facts).await?;

    println!("{:?}", rule_results);
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-11-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Rust语言学习交流 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • json-rules-engine
    • 项目地址
    • 安装
    • 特性
    • 实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档