rush_core/
define.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::sync::Arc;
4
5// 核心抽象,所有规则算法都可以看作是一个过滤器,给如一个输入,给出一个输出
6pub trait RuleFlow {
7    fn version(&self) -> i32 {
8        1
9    }
10    fn flow<Obj: Serialize, Out: for<'a> Deserialize<'a>>(&self, obj: Obj) -> anyhow::Result<Out>;
11}
12
13#[async_trait::async_trait]
14pub trait AsyncRuleFlow: RuleFlow + Sync + Send {
15    async fn async_flow<Obj: Serialize + Send, Out: for<'a> Deserialize<'a>>(
16        &self,
17        obj: Obj,
18    ) -> anyhow::Result<Out> {
19        self.flow(obj)
20    }
21}
22
23// 计算节点
24pub trait CalcNode: Send + Sync {
25    fn when(&self, fs: Arc<dyn FunctionSet>, input: &Value) -> anyhow::Result<bool>;
26}
27// 运算规则
28pub trait Exec: Send + Sync {
29    fn execute(
30        &self,
31        fs: Arc<dyn FunctionSet>,
32        input: &Value,
33        output: &mut Value,
34    ) -> anyhow::Result<()>;
35}
36// 函数
37pub trait Function: Send + Sync {
38    fn call(&self, fs: Arc<dyn FunctionSet>, args: Vec<Value>) -> anyhow::Result<Value>;
39}
40// 函数集
41pub trait FunctionSet: Send + Sync {
42    fn get(&self, name: &str) -> Option<Arc<dyn Function>>;
43}
44
45#[async_trait::async_trait]
46pub trait RuleEngineDiscovery<F> {
47    fn version(&self) -> i32 {
48        1
49    }
50    async fn upgrade(&self) -> F;
51}