mould_extension_sdk/
lib.rs

1pub use async_trait;
2pub mod pluginator;
3use serde::{Deserialize, Serialize};
4pub use serde_json;
5use serde_json::Value;
6use std::future::Future;
7use std::pin::Pin;
8use std::sync::Arc;
9
10#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
11pub enum LogLevel {
12    Error = 1,
13    Warn,
14    Info,
15    Debug,
16    Trace,
17}
18
19pub struct Operation {
20    pub id: String,
21    pub name: String,
22    pub parameter_schema: Vec<Attribute>,
23}
24
25pub struct EnumOption {
26    pub value: String,
27    pub label: String,
28}
29
30#[derive(Serialize, Deserialize, Debug, Clone)]
31pub struct File {
32    pub key: String,
33    pub name: String,
34    pub size: f64,
35    pub mime_type: String,
36}
37
38pub enum AttributeType {
39    String,
40    StringList,
41    LongString,
42    // RichText,
43    Code { language: String },
44    Password,
45    Enum { options: Vec<EnumOption> },
46    EnumList { options: Vec<EnumOption> },
47    Bool,
48    //序列化成对象,属性有key, name, size, mime_type
49    File,
50    //序列化成数组,数组对象的属性有key, name, size, mime_type
51    FileList,
52}
53
54pub struct Attribute {
55    pub id: String,
56    pub name: String,
57    pub description: Option<String>,
58    pub required: bool,
59    pub r#type: AttributeType,
60}
61
62#[async_trait::async_trait]
63pub trait ContextTrait {
64    async fn spawn_blocking(&self, task: Box<dyn FnOnce() + Send + 'static>) -> Result<(), String>;
65    fn spawn_future(
66        &self,
67        future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
68    ) -> Result<(), String>;
69    fn modify_json_custom(
70        &self,
71        target: Value,
72        json_path: &str,
73        js_func: &str,
74        resource_index: u32,
75    ) -> Result<Value, String>;
76    fn modify_json(
77        &self,
78        target: Value,
79        json_path: &str,
80        new_value: Value,
81    ) -> Result<Value, String>;
82    async fn download_file(&self, key: &str) -> Result<std::fs::File, String>;
83}
84
85pub type Context = Arc<dyn ContextTrait + Send + Sync>;
86pub type AppendLog = Arc<dyn Fn(LogLevel, String) + Send + Sync>;
87
88#[async_trait::async_trait]
89pub trait Extension: Sync + Send + 'static {
90    //扩展的id,类似包名,唯一表示该扩展
91    fn id(&self) -> String;
92    //扩展的名称
93    fn name(&self) -> String;
94    //扩展所需的配置
95    fn configuration_schema(&self) -> Vec<Attribute>;
96    //检查配置是否合格
97    fn validate_configuration(&self, configuration: Value) -> Result<(), String>;
98    //测试配置(主要是网络连通性)
99    async fn test_configuration(
100        &self,
101        _configuration: Value,
102        _context: &Context,
103    ) -> Result<(), String> {
104        return Ok(());
105    }
106    //检查操作参数是否合格
107    fn validate_operation_parameter(
108        &self,
109        operation_id: &str,
110        operation_parameter: Value,
111    ) -> Result<(), String>;
112    //该扩展可以执行哪些操作
113    fn operations(&self) -> Vec<Operation>;
114    //执行对应的操作
115    async fn handle(
116        &self,
117        configuration: Value,
118        operation_id: &str,
119        operation_parameter: Value,
120        context: &Context,
121        append_log: &AppendLog,
122        resource_index: u32,
123    ) -> Result<(), String>;
124}
125
126plugin_trait!(Extension);