Skip to main content

systemprompt_models/modules/
types.rs

1use serde::{Deserialize, Deserializer, Serialize};
2use std::path::PathBuf;
3pub use systemprompt_extension::{SchemaSource, SeedSource};
4
5use crate::errors::ModuleError;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Module {
9    #[serde(skip_deserializing, default = "generate_uuid")]
10    pub uuid: String,
11    pub name: String,
12    pub version: String,
13    pub display_name: String,
14    pub description: Option<String>,
15    pub weight: Option<i32>,
16    #[serde(default)]
17    pub dependencies: Vec<String>,
18    pub schemas: Option<Vec<ModuleSchema>>,
19    pub seeds: Option<Vec<ModuleSeed>>,
20    pub permissions: Option<Vec<ModulePermission>>,
21    #[serde(default)]
22    pub audience: Vec<String>,
23    #[serde(skip_deserializing, default)]
24    pub enabled: bool,
25    #[serde(default)]
26    pub api: Option<ApiConfig>,
27    #[serde(skip)]
28    pub path: PathBuf,
29}
30
31impl Module {
32    pub fn parse(content: &str, module_path: PathBuf) -> Result<Self, ModuleError> {
33        let mut module: Self = serde_yaml::from_str(content)?;
34        module.path = module_path;
35        Ok(module)
36    }
37}
38
39fn generate_uuid() -> String {
40    uuid::Uuid::new_v4().to_string()
41}
42
43pub type ModuleDefinition = Module;
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct ApiConfig {
47    pub enabled: bool,
48    #[serde(default)]
49    pub path_prefix: Option<String>,
50    #[serde(default)]
51    pub openapi_path: Option<String>,
52}
53
54#[derive(Debug, Clone, Serialize)]
55pub struct ModuleSchema {
56    pub sql: SchemaSource,
57    pub table: String,
58    #[serde(default)]
59    pub required_columns: Vec<String>,
60}
61
62#[derive(Deserialize)]
63struct ModuleSchemaYaml {
64    file: String,
65    table: String,
66    #[serde(default)]
67    required_columns: Vec<String>,
68}
69
70impl<'de> Deserialize<'de> for ModuleSchema {
71    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
72    where
73        D: Deserializer<'de>,
74    {
75        let yaml = ModuleSchemaYaml::deserialize(deserializer)?;
76        Ok(Self {
77            sql: SchemaSource::File(PathBuf::from(yaml.file)),
78            table: yaml.table,
79            required_columns: yaml.required_columns,
80        })
81    }
82}
83
84#[derive(Debug, Clone, Serialize)]
85pub struct ModuleSeed {
86    pub sql: SeedSource,
87    pub table: String,
88    pub check_column: String,
89    pub check_value: String,
90}
91
92#[derive(Deserialize)]
93struct ModuleSeedYaml {
94    file: String,
95    table: String,
96    check_column: String,
97    check_value: String,
98}
99
100impl<'de> Deserialize<'de> for ModuleSeed {
101    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
102    where
103        D: Deserializer<'de>,
104    {
105        let yaml = ModuleSeedYaml::deserialize(deserializer)?;
106        Ok(Self {
107            sql: SeedSource::File(PathBuf::from(yaml.file)),
108            table: yaml.table,
109            check_column: yaml.check_column,
110            check_value: yaml.check_value,
111        })
112    }
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct ModulePermission {
117    pub name: String,
118    pub description: String,
119    pub resource: String,
120    pub action: String,
121}
122
123#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub enum ModuleType {
125    Regular,
126    Proxy,
127}