use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use crate::error::PluginError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginManifest {
pub plugin: PluginMeta,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginMeta {
pub name: String,
pub version: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub author: String,
#[serde(default)]
pub library: Option<String>,
#[serde(default)]
pub config: Option<toml::Value>,
}
impl PluginManifest {
pub fn load(path: &Path) -> Result<Self, PluginError> {
let content = std::fs::read_to_string(path)?;
toml::from_str(&content).map_err(|e| PluginError::ManifestError(e.to_string()))
}
pub fn parse_toml(content: &str) -> Result<Self, PluginError> {
toml::from_str(content).map_err(|e| PluginError::ManifestError(e.to_string()))
}
pub fn library_path(&self, base_dir: &Path) -> PathBuf {
if let Some(ref lib) = self.plugin.library {
let lib_path = PathBuf::from(lib);
if lib_path.is_absolute() {
lib_path
} else {
base_dir.join(lib)
}
} else {
let ext = platform_library_extension();
let filename = if cfg!(target_os = "windows") {
format!("{}{}", self.plugin.name, ext)
} else {
format!("lib{}{}", self.plugin.name, ext)
};
base_dir.join(filename)
}
}
pub fn config_json(&self) -> String {
match &self.plugin.config {
Some(value) => serde_json::to_string(value).unwrap_or_else(|_| "{}".to_string()),
None => "{}".to_string(),
}
}
}
pub fn platform_library_extension() -> &'static str {
if cfg!(target_os = "linux") {
".so"
} else if cfg!(target_os = "macos") {
".dylib"
} else if cfg!(target_os = "windows") {
".dll"
} else {
".so" }
}
pub fn discover_plugins(plugin_dir: &Path) -> Vec<Result<(PathBuf, PluginManifest), PluginError>> {
let mut results = Vec::new();
let entries = match std::fs::read_dir(plugin_dir) {
Ok(entries) => entries,
Err(_) => return results,
};
for entry in entries.flatten() {
let path = entry.path();
if !path.is_dir() {
continue;
}
let manifest_path = path.join("manifest.toml");
if !manifest_path.exists() {
continue;
}
results.push(PluginManifest::load(&manifest_path).map(|m| (path.clone(), m)));
}
results
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_minimal_manifest() {
let toml_str = r#"
[plugin]
name = "echo"
version = "1.0.0"
"#;
let manifest = PluginManifest::parse_toml(toml_str).unwrap();
assert_eq!(manifest.plugin.name, "echo");
assert_eq!(manifest.plugin.version, "1.0.0");
assert!(manifest.plugin.description.is_empty());
assert!(manifest.plugin.library.is_none());
assert!(manifest.plugin.config.is_none());
}
#[test]
fn test_parse_full_manifest() {
let toml_str = r#"
[plugin]
name = "sentiment"
version = "2.1.0"
description = "Sentiment analysis plugin"
author = "Someone"
library = "libsentiment_ext.so"
[plugin.config]
model = "distilbert"
threshold = 0.7
"#;
let manifest = PluginManifest::parse_toml(toml_str).unwrap();
assert_eq!(manifest.plugin.name, "sentiment");
assert_eq!(manifest.plugin.version, "2.1.0");
assert_eq!(manifest.plugin.description, "Sentiment analysis plugin");
assert_eq!(
manifest.plugin.library.as_deref(),
Some("libsentiment_ext.so")
);
assert!(manifest.plugin.config.is_some());
}
#[test]
fn test_config_json() {
let toml_str = r#"
[plugin]
name = "test"
version = "0.1.0"
[plugin.config]
key = "value"
count = 42
"#;
let manifest = PluginManifest::parse_toml(toml_str).unwrap();
let json = manifest.config_json();
assert!(json.contains("key"));
assert!(json.contains("value"));
}
#[test]
fn test_config_json_empty() {
let toml_str = r#"
[plugin]
name = "test"
version = "0.1.0"
"#;
let manifest = PluginManifest::parse_toml(toml_str).unwrap();
assert_eq!(manifest.config_json(), "{}");
}
#[test]
fn test_library_path_auto_infer() {
let toml_str = r#"
[plugin]
name = "echo"
version = "1.0.0"
"#;
let manifest = PluginManifest::parse_toml(toml_str).unwrap();
let dir = PathBuf::from("/opt/app/plugins/echo");
let lib_path = manifest.library_path(&dir);
if cfg!(target_os = "windows") {
assert!(lib_path.ends_with("echo.dll"));
assert_eq!(lib_path, PathBuf::from("/opt/app/plugins/echo/echo.dll"));
} else {
assert!(lib_path.ends_with("libecho.so"));
assert_eq!(lib_path, PathBuf::from("/opt/app/plugins/echo/libecho.so"));
}
}
#[test]
fn test_library_path_explicit() {
let toml_str = r#"
[plugin]
name = "custom"
version = "1.0.0"
library = "my_custom_lib.so"
"#;
let manifest = PluginManifest::parse_toml(toml_str).unwrap();
let dir = PathBuf::from("/opt/app/plugins");
let lib_path = manifest.library_path(&dir);
assert!(lib_path.to_str().unwrap().contains("my_custom_lib.so"));
}
#[test]
fn test_discover_missing_dir() {
let results = discover_plugins(Path::new("/nonexistent/path/xyz"));
assert!(results.is_empty());
}
}