moduforge_core/state/
plugin.rs

1use async_trait::async_trait;
2use std::sync::Arc;
3
4use super::state::{State, StateConfig};
5use super::transaction::Transaction;
6
7/// 插件特征
8/// 定义插件的核心行为,包括事务处理和过滤功能
9#[async_trait]
10pub trait PluginTrait: Send + Sync + Debug {
11    /// 追加事务处理
12    /// 允许插件在事务执行前修改或扩展事务内容
13    async fn append_transaction(
14        &self,
15        _: &Transaction,
16        _: &State,
17        _: &State,
18    ) -> Option<Transaction> {
19        None
20    }
21    /// 事务过滤
22    /// 决定是否允许事务执行
23    async fn filter_transaction(
24        &self,
25        _: &Transaction,
26        _: &State,
27    ) -> bool {
28        true
29    }
30
31    /// 事务应用前的处理
32    async fn before_apply_transaction(
33        &self,
34        _tr: &mut Transaction,
35        _state: &State,
36    ) -> Result<(), Box<dyn std::error::Error>> {
37        Ok(())
38    }
39
40    /// 事务应用后的处理
41    async fn after_apply_transaction(
42        &self,
43        _new_state: &State,
44        _tr: &mut Transaction,
45        _old_state: &State,
46    ) -> Result<(), Box<dyn std::error::Error>> {
47        Ok(())
48    }
49}
50/// 状态字段特征
51/// 定义插件状态的管理方式,包括初始化、应用更改和序列化
52#[async_trait]
53pub trait StateField: Send + Sync + Debug {
54    /// 初始化插件状态
55    async fn init(
56        &self,
57        config: &StateConfig,
58        instance: Option<&State>,
59    ) -> PluginState;
60    /// 应用状态变更
61    /// 根据事务内容更新插件状态
62    async fn apply(
63        &self,
64        tr: &Transaction,
65        value: PluginState,
66        old_state: &State,
67        new_state: &State,
68    ) -> PluginState;
69    /// 序列化插件状态
70    fn serialize(
71        &self,
72        _value: PluginState,
73    ) -> Option<Vec<u8>> {
74        None
75    }
76    /// 反序列化插件状态
77    fn deserialize(
78        &self,
79        _value: &Vec<u8>,
80    ) -> Option<PluginState> {
81        None
82    }
83}
84/// 插件规范结构体
85/// 定义插件的配置和行为
86#[derive(Clone, Debug)]
87pub struct PluginSpec {
88    pub state: Option<Arc<dyn StateField>>,
89    pub key: PluginKey,
90    pub tr: Option<Arc<dyn PluginTrait>>,
91}
92impl PluginSpec {
93    /// 插件状态管理器
94    async fn filter_transaction(
95        &self,
96        tr: &Transaction,
97        state: &State,
98    ) -> bool {
99        match &self.tr {
100            Some(filter) => filter.filter_transaction(tr, state).await,
101            None => false,
102        }
103    }
104    /// 执行事务追加
105    async fn append_transaction<'a>(
106        &self,
107        trs: &Transaction,
108        old_state: &State,
109        new_state: &State,
110    ) -> Option<Transaction> {
111        match &self.tr {
112            Some(transaction) => transaction.append_transaction(trs, old_state, new_state).await,
113            None => None,
114        }
115    }
116    pub async fn before_apply_transaction(
117        &self,
118        tr: &mut Transaction,
119        state: &State,
120    ) -> Result<(), Box<dyn std::error::Error>> {
121        // 默认实现为空,由具体插件重写
122        if let Some(transaction) = &self.tr {
123            transaction.before_apply_transaction(tr, state).await?;
124        }
125        Ok(())
126    }
127
128    /// 事务应用后的处理
129    pub async fn after_apply_transaction(
130        &self,
131        new_state: &State,
132        tr: &mut Transaction,
133        old_state: &State,
134    ) -> Result<(), Box<dyn std::error::Error>> {
135        // 默认实现为空,由具体插件重写
136        if let Some(transaction) = &self.tr {
137            transaction.after_apply_transaction(new_state, tr, old_state).await?;
138        }
139        Ok(())
140    }
141}
142/// 插件实例结构体
143/// 表示一个具体的插件实例
144#[derive(Clone, Debug)]
145pub struct Plugin {
146    pub spec: PluginSpec,
147    pub key: String,
148}
149
150impl Plugin {
151    /// 创建新的插件实例
152    pub fn new(spec: PluginSpec) -> Self {
153        let key = spec.key.0.clone();
154
155        Plugin { spec, key }
156    }
157
158    /// 从全局状态中获取插件状态
159    pub fn get_state(
160        &self,
161        state: &State,
162    ) -> Option<PluginState> {
163        state.get_field(&self.key)
164    }
165    /// 应用事务过滤逻辑
166    pub async fn apply_filter_transaction(
167        &self,
168        tr: &Transaction,
169        state: &State,
170    ) -> bool {
171        self.spec.filter_transaction(tr, state).await
172    }
173
174    /// 事务应用前的处理
175    pub async fn before_apply_transaction(
176        &self,
177        tr: &mut Transaction,
178        state: &State,
179    ) -> Result<(), Box<dyn std::error::Error>> {
180        // 默认实现为空,由具体插件重写
181        self.spec.before_apply_transaction(tr, state).await?;
182        Ok(())
183    }
184
185    /// 事务应用后的处理
186    pub async fn after_apply_transaction(
187        &self,
188        new_state: &State,
189        tr: &mut Transaction,
190        old_state: &State,
191    ) -> Result<(), Box<dyn std::error::Error>> {
192        // 默认实现为空,由具体插件重写
193        self.spec.after_apply_transaction(new_state, tr, old_state).await?;
194        Ok(())
195    }
196
197    /// 应用事务追加逻辑
198    pub async fn apply_append_transaction(
199        &self,
200        trs: &Transaction,
201        old_state: &State,
202        new_state: &State,
203    ) -> Option<Transaction> {
204        self.spec.append_transaction(trs, old_state, new_state).await
205    }
206}
207
208/// 插件状态类型
209/// 使用 Arc 包装的任意类型作为插件状态
210pub type PluginState = Arc<dyn std::any::Any + Send + Sync>;
211
212use std::fmt::Debug;
213
214/// 插件键类型
215/// 使用两个字符串组成的元组作为插件的唯一标识
216pub type PluginKey = (String, String);