mofa_plugins/rhai_runtime/
types.rs1use anyhow::Result;
6use rhai::Dynamic;
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9use std::path::PathBuf;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct PluginMetadata {
18 pub id: String,
20 pub name: String,
22 pub version: String,
24 pub description: String,
26 pub author: Option<String>,
28 pub homepage: Option<String>,
30 pub capabilities: Vec<String>,
32 pub dependencies: Vec<String>,
34 pub created_at: u64,
36 pub modified_at: u64,
38 pub path: Option<PathBuf>,
40}
41
42impl Default for PluginMetadata {
43 fn default() -> Self {
44 let now = chrono::Utc::now().timestamp() as u64;
45 Self {
46 id: uuid::Uuid::now_v7().to_string(),
47 name: "unknown".to_string(),
48 version: "0.0.0".to_string(),
49 description: "".to_string(),
50 author: None,
51 homepage: None,
52 capabilities: Vec::new(),
53 dependencies: Vec::new(),
54 created_at: now,
55 modified_at: now,
56 path: None,
57 }
58 }
59}
60
61impl PluginMetadata {
62 pub fn new(id: &str, name: &str, version: &str) -> Self {
64 let mut meta = Self::default();
65 meta.id = id.to_string();
66 meta.name = name.to_string();
67 meta.version = version.to_string();
68 meta
69 }
70
71 pub fn from_rhai_vars(_vars: &HashMap<String, Dynamic>) -> Result<Self> {
73 Ok(Self::default())
75 }
76}
77
78#[derive(thiserror::Error, Debug)]
84pub enum RhaiPluginError {
85 #[error("Compilation error: {0}")]
87 CompilationError(String),
88
89 #[error("Execution error: {0}")]
91 ExecutionError(String),
92
93 #[error("Missing required function: {0}")]
95 MissingFunction(String),
96
97 #[error("Invalid metadata format: {0}")]
99 InvalidMetadata(String),
100
101 #[error("File IO error: {0}")]
103 IoError(#[from] std::io::Error),
104
105 #[error("Rhai error: {0}")]
107 RhaiError(String),
108
109 #[error("JSON error: {0}")]
111 JsonError(#[from] serde_json::Error),
112
113 #[error("Other: {0}")]
115 Other(#[from] anyhow::Error),
116}
117
118pub type RhaiPluginResult<T = ()> = Result<T, RhaiPluginError>;
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
127pub enum PluginCapability {
128 Execution,
130 FileSystem,
132 Network,
134 SystemCommand,
136 EventSubscription,
138 PluginManagement,
140}
141
142impl ToString for PluginCapability {
143 fn to_string(&self) -> String {
144 match self {
145 PluginCapability::Execution => "execution",
146 PluginCapability::FileSystem => "file_system",
147 PluginCapability::Network => "network",
148 PluginCapability::SystemCommand => "system_command",
149 PluginCapability::EventSubscription => "event_subscription",
150 PluginCapability::PluginManagement => "plugin_management",
151 }
152 .to_string()
153 }
154}