use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use oxidite_core::{Error, Result};
#[async_trait]
pub trait Plugin: Send + Sync {
fn info(&self) -> PluginInfo;
async fn on_load(&self) -> Result<()> {
Ok(())
}
async fn on_unload(&self) -> Result<()> {
Ok(())
}
async fn on_enable(&self) -> Result<()> {
Ok(())
}
async fn on_disable(&self) -> Result<()> {
Ok(())
}
async fn on_startup(&self) -> Result<()> {
Ok(())
}
async fn on_shutdown(&self) -> Result<()> {
Ok(())
}
async fn hook(&self, _hook: PluginHook) -> HookResult {
HookResult::Continue
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginInfo {
pub id: String,
pub name: String,
pub version: String,
pub description: String,
pub author: String,
pub license: String,
pub homepage: Option<String>,
pub repository: Option<String>,
pub enabled: bool,
}
impl PluginInfo {
pub fn new(id: &str, name: &str, version: &str, description: &str, author: &str) -> Self {
Self {
id: id.to_string(),
name: name.to_string(),
version: version.to_string(),
description: description.to_string(),
author: author.to_string(),
license: "MIT".to_string(),
homepage: None,
repository: None,
enabled: false,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PluginHook {
PreRequest { path: String, method: String },
PostResponse { path: String, method: String, status: u16 },
OnAuth { user_id: String },
OnLogout { user_id: String },
OnModelCreate { model: String, id: String },
OnModelUpdate { model: String, id: String },
OnModelDelete { model: String, id: String },
Custom { name: String, data: serde_json::Value },
}
#[derive(Debug)]
pub enum HookResult {
Continue,
Stop,
Response(String),
Error(Error),
Transform(serde_json::Value),
}