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