1use async_trait::async_trait;
2use serde::{Deserialize, Serialize};
3use oxidite_core::{OxiditeResponse, Error, Result};
4
5#[async_trait]
10pub trait Plugin: Send + Sync {
11 fn info(&self) -> PluginInfo;
13
14 async fn on_load(&self) -> Result<()> {
16 Ok(())
17 }
18
19 async fn on_unload(&self) -> Result<()> {
21 Ok(())
22 }
23
24 async fn on_enable(&self) -> Result<()> {
26 Ok(())
27 }
28
29 async fn on_disable(&self) -> Result<()> {
31 Ok(())
32 }
33
34 async fn on_startup(&self) -> Result<()> {
36 Ok(())
37 }
38
39 async fn on_shutdown(&self) -> Result<()> {
41 Ok(())
42 }
43
44 async fn hook(&self, hook: PluginHook) -> HookResult {
46 HookResult::Continue
47 }
48}
49
50#[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#[derive(Debug, Clone, Serialize, Deserialize)]
82pub enum PluginHook {
83 PreRequest { path: String, method: String },
85
86 PostResponse { path: String, method: String, status: u16 },
88
89 OnAuth { user_id: String },
91
92 OnLogout { user_id: String },
94
95 OnModelCreate { model: String, id: String },
97
98 OnModelUpdate { model: String, id: String },
100
101 OnModelDelete { model: String, id: String },
103
104 Custom { name: String, data: serde_json::Value },
106}
107
108#[derive(Debug)]
111pub enum HookResult {
112 Continue,
114
115 Stop,
117
118 Response(String),
120
121 Error(Error),
123
124 Transform(serde_json::Value),
126}