Skip to main content

oxidite_plugin/
plugin.rs

1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use oxidite_core::{OxiditeResponse, Error, Result};
4
5// Don't import the types that cause conflicts
6
7
8/// Plugin trait that defines the interface for all plugins
9#[async_trait]
10pub trait Plugin: Send + Sync {
11    /// Get plugin information
12    fn info(&self) -> PluginInfo;
13    
14    /// Called when the plugin is loaded
15    async fn on_load(&self) -> Result<()> {
16        Ok(())
17    }
18    
19    /// Called when the plugin is unloaded
20    async fn on_unload(&self) -> Result<()> {
21        Ok(())
22    }
23    
24    /// Called when the plugin is enabled
25    async fn on_enable(&self) -> Result<()> {
26        Ok(())
27    }
28    
29    /// Called when the plugin is disabled
30    async fn on_disable(&self) -> Result<()> {
31        Ok(())
32    }
33    
34    /// Called before the application starts
35    async fn on_startup(&self) -> Result<()> {
36        Ok(())
37    }
38    
39    /// Called after the application shuts down
40    async fn on_shutdown(&self) -> Result<()> {
41        Ok(())
42    }
43    
44    /// Hook into various parts of the application lifecycle
45    async fn hook(&self, hook: PluginHook) -> HookResult {
46        HookResult::Continue
47    }
48}
49
50/// Information about a plugin
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct PluginInfo {
53    pub id: String,
54    pub name: String,
55    pub version: String,
56    pub description: String,
57    pub author: String,
58    pub license: String,
59    pub homepage: Option<String>,
60    pub repository: Option<String>,
61    pub enabled: bool,
62}
63
64impl PluginInfo {
65    pub fn new(id: &str, name: &str, version: &str, description: &str, author: &str) -> Self {
66        Self {
67            id: id.to_string(),
68            name: name.to_string(),
69            version: version.to_string(),
70            description: description.to_string(),
71            author: author.to_string(),
72            license: "MIT".to_string(),
73            homepage: None,
74            repository: None,
75            enabled: false,
76        }
77    }
78}
79
80/// Different hooks that plugins can implement
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub enum PluginHook {
83    /// Called before a request is processed
84    PreRequest { path: String, method: String },
85    
86    /// Called after a response is generated
87    PostResponse { path: String, method: String, status: u16 },
88    
89    /// Called when a user authenticates
90    OnAuth { user_id: String },
91    
92    /// Called when a user logs out
93    OnLogout { user_id: String },
94    
95    /// Called when a model is created
96    OnModelCreate { model: String, id: String },
97    
98    /// Called when a model is updated
99    OnModelUpdate { model: String, id: String },
100    
101    /// Called when a model is deleted
102    OnModelDelete { model: String, id: String },
103    
104    /// Custom hook with arbitrary data
105    Custom { name: String, data: serde_json::Value },
106}
107
108/// Result of a hook execution
109// Changed to avoid Debug requirement on OxiditeResponse
110#[derive(Debug)]
111pub enum HookResult {
112    /// Continue with normal execution
113    Continue,
114    
115    /// Stop execution and return early
116    Stop,
117    
118    /// Return a modified response - using a placeholder to avoid Debug issue
119    Response(String),
120    
121    /// Return an error
122    Error(Error),
123    
124    /// Transform data in the hook chain
125    Transform(serde_json::Value),
126}