feat: add __rule, but cannot run
This commit is contained in:
@@ -50,7 +50,8 @@ Project or files:
|
|||||||
├── __linux
|
├── __linux
|
||||||
│ ├── notify
|
│ ├── notify
|
||||||
│ ├── psutil
|
│ ├── psutil
|
||||||
│ └── sysinfo
|
│ ├── sysinfo
|
||||||
|
│ └── unixsocket
|
||||||
├── __log
|
├── __log
|
||||||
│ └── log4rs
|
│ └── log4rs
|
||||||
├── __misc
|
├── __misc
|
||||||
@@ -69,6 +70,8 @@ Project or files:
|
|||||||
│ └── udp_laminar
|
│ └── udp_laminar
|
||||||
├── __performance
|
├── __performance
|
||||||
│ └── print_perf
|
│ └── print_perf
|
||||||
|
├── __rule
|
||||||
|
│ └── json-rules-engine
|
||||||
├── __search
|
├── __search
|
||||||
│ ├── simsearch
|
│ ├── simsearch
|
||||||
│ └── tantivy
|
│ └── tantivy
|
||||||
@@ -133,7 +136,7 @@ Project or files:
|
|||||||
├── vec.rs
|
├── vec.rs
|
||||||
└── while.rs
|
└── while.rs
|
||||||
|
|
||||||
105 directories, 26 files
|
108 directories, 26 files
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
1504
__rule/json-rules-engine/Cargo.lock
generated
Normal file
1504
__rule/json-rules-engine/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
__rule/json-rules-engine/Cargo.toml
Normal file
15
__rule/json-rules-engine/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "json-rules-engine"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
json-rules-engine = { version = "0.7.0", features = ["email"] }
|
||||||
|
tokio = { version = "0.3.3", features = ["full"] }
|
||||||
|
serde = "*"
|
||||||
|
serde_json = { version = "*" }
|
||||||
|
anyhow = { version = "*" }
|
||||||
|
|
||||||
9
__rule/json-rules-engine/README.md
Normal file
9
__rule/json-rules-engine/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
Project url:
|
||||||
|
https://github.com/GopherJ/json-rules-engine-rs
|
||||||
|
|
||||||
|
|
||||||
|
https://rustcc.cn/article?id=0e1248f5-7965-41a4-a469-bad0befc136a
|
||||||
|
|
||||||
|
got panic, why?
|
||||||
|
> thread 'main' panicked at 'not currently running on the Tokio runtime.'
|
||||||
86
__rule/json-rules-engine/src/main.rs
Normal file
86
__rule/json-rules-engine/src/main.rs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
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]
|
||||||
|
pub async fn 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.into());
|
||||||
|
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);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user