use crate::{IconDescriptor, PluginError, PluginResult};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ManifestToolbarButton {
pub button_id: String,
pub tooltip: String,
pub action_id: String,
pub icon_svg: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PluginEnvironment {
pub version: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct PluginManifest {
pub id: String,
pub name: String,
pub version: String,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub repository: Option<String>,
#[serde(default)]
pub entry_ui: Option<String>,
#[serde(default)]
pub entry_component: Option<String>,
pub icon: Option<IconDescriptor>,
#[serde(default)]
pub toolbar_buttons: Vec<ManifestToolbarButton>,
pub environment: PluginEnvironment,
}
pub const MANIFEST_FILENAME: &str = "plugin.toml";
impl PluginManifest {
pub fn from_toml(toml_str: &str, plugin_id_hint: &str) -> PluginResult<Self> {
let manifest: Self = toml::from_str(toml_str).map_err(|e| PluginError::Manifest {
plugin_id: plugin_id_hint.to_string(),
message: format!("TOML parse error: {e}"),
})?;
manifest.validate()?;
Ok(manifest)
}
pub fn from_file(path: &Path) -> PluginResult<Self> {
let dir_name = path
.parent()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str())
.unwrap_or("<unknown>");
let contents = std::fs::read_to_string(path).map_err(|e| PluginError::Manifest {
plugin_id: dir_name.to_string(),
message: format!("failed to read {}: {e}", path.display()),
})?;
Self::from_toml(&contents, dir_name)
}
fn validate(&self) -> PluginResult<()> {
if self.id.is_empty() {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: "'id' must not be empty".into(),
});
}
if self.name.is_empty() {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: "'name' must not be empty".into(),
});
}
if self.version.is_empty() {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: "'version' must not be empty".into(),
});
}
if let Some(ref entry_ui) = self.entry_ui {
if entry_ui.is_empty() {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: "'entry_ui' must not be empty when specified".into(),
});
}
if Path::new(entry_ui).is_absolute() {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: format!("'entry_ui' must be a relative path, got '{entry_ui}'"),
});
}
if entry_ui.contains("..") {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: format!("'entry_ui' must not contain '..', got '{entry_ui}'"),
});
}
}
if let Some(ref entry_component) = self.entry_component
&& entry_component.is_empty()
{
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: "'entry_component' must not be empty when specified".into(),
});
}
if let Some(IconDescriptor::File { path }) = &self.icon {
if path.is_absolute() {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: format!("icon file path must be relative, got '{}'", path.display()),
});
}
if path.to_string_lossy().contains("..") {
return Err(PluginError::Manifest {
plugin_id: self.id.clone(),
message: format!(
"icon file path must not contain '..', got '{}'",
path.display()
),
});
}
}
Ok(())
}
pub fn resolve_entry_ui(&self, plugin_root: &Path) -> Option<PathBuf> {
self.entry_ui.as_ref().map(|ui| plugin_root.join(ui))
}
pub fn validate_files(&self, plugin_root: &Path) -> PluginResult<()> {
if let Some(ui_path) = self.resolve_entry_ui(plugin_root)
&& !ui_path.exists()
{
return Err(PluginError::MissingFile {
plugin_id: self.id.clone(),
path: ui_path,
});
}
if let Some(IconDescriptor::File { path }) = &self.icon {
let icon_path = plugin_root.join(path);
if !icon_path.exists() {
return Err(PluginError::MissingFile {
plugin_id: self.id.clone(),
path: icon_path,
});
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
const VALID_TOML: &str = r#"
id = "test_plugin"
name = "Test Plugin"
version = "1.0.0"
description = "A test plugin"
repository = "https://github.com/example/test-plugin"
entry_ui = "ui/panel.slint"
entry_component = "Panel"
[environment]
version = ">=0.4.1"
[icon]
kind = "svg"
data = "<svg/>"
"#;
#[test]
fn parse_valid_manifest() {
let m = PluginManifest::from_toml(VALID_TOML, "test").unwrap();
assert_eq!(m.id, "test_plugin");
assert_eq!(m.name, "Test Plugin");
assert_eq!(m.version, "1.0.0");
assert_eq!(m.description.as_deref(), Some("A test plugin"));
assert_eq!(
m.repository.as_deref(),
Some("https://github.com/example/test-plugin")
);
assert_eq!(m.entry_ui.as_deref(), Some("ui/panel.slint"));
assert_eq!(m.entry_component.as_deref(), Some("Panel"));
assert_eq!(
m.icon,
Some(IconDescriptor::Svg {
data: "<svg/>".into()
})
);
}
#[test]
fn reject_missing_id() {
let toml = r#"
name = "Test"
version = "1.0.0"
entry_ui = "ui/p.slint"
entry_component = "P"
[environment]
version = ">=0.4.1"
"#;
let err = PluginManifest::from_toml(toml, "hint").unwrap_err();
assert!(err.to_string().contains("TOML parse error"));
}
#[test]
fn reject_empty_id() {
let toml = r#"
id = ""
name = "Test"
version = "1.0.0"
entry_ui = "ui/p.slint"
entry_component = "P"
[environment]
version = ">=0.4.1"
"#;
let err = PluginManifest::from_toml(toml, "hint").unwrap_err();
assert!(err.to_string().contains("'id' must not be empty"));
}
#[test]
fn reject_absolute_entry_ui() {
let toml = r#"
id = "abs"
name = "Test"
version = "1.0.0"
entry_ui = "/etc/evil.slint"
entry_component = "Evil"
[environment]
version = ">=0.4.1"
"#;
let err = PluginManifest::from_toml(toml, "hint").unwrap_err();
assert!(err.to_string().contains("relative path"));
}
#[test]
fn reject_path_traversal_in_entry_ui() {
let toml = r#"
id = "trav"
name = "Test"
version = "1.0.0"
entry_ui = "../escape/evil.slint"
entry_component = "Evil"
[environment]
version = ">=0.4.1"
"#;
let err = PluginManifest::from_toml(toml, "hint").unwrap_err();
assert!(err.to_string().contains(".."));
}
#[test]
fn resolve_entry_ui_relative_to_root() {
let m = PluginManifest::from_toml(VALID_TOML, "test").unwrap();
let resolved = m.resolve_entry_ui(Path::new("/plugins/test_plugin"));
assert_eq!(
resolved,
Some(PathBuf::from("/plugins/test_plugin/ui/panel.slint"))
);
}
#[test]
fn validate_files_missing_ui() {
let m = PluginManifest::from_toml(VALID_TOML, "test").unwrap();
let err = m
.validate_files(Path::new("/nonexistent/plugin/root"))
.unwrap_err();
match err {
PluginError::MissingFile { plugin_id, .. } => {
assert_eq!(plugin_id, "test_plugin");
}
other => panic!("expected MissingFile, got {other:?}"),
}
}
#[test]
fn parse_manifest_without_icon() {
let toml = r#"
id = "no_icon"
name = "No Icon Plugin"
version = "0.1.0"
entry_ui = "ui/panel.slint"
entry_component = "Panel"
[environment]
version = ">=0.4.1"
"#;
let m = PluginManifest::from_toml(toml, "no_icon").unwrap();
assert!(m.icon.is_none());
}
#[test]
fn parse_manifest_without_optional_metadata() {
let m = PluginManifest::from_toml(
r#"
id = "legacy"
name = "Legacy"
version = "0.1.0"
[environment]
version = ">=0.4.1"
"#,
"legacy",
)
.unwrap();
assert!(m.description.is_none());
assert!(m.repository.is_none());
}
#[test]
fn reject_absolute_icon_path() {
let toml = r#"
id = "bad_icon"
name = "Test"
version = "1.0.0"
entry_ui = "ui/p.slint"
entry_component = "P"
[environment]
version = ">=0.4.1"
[icon]
kind = "file"
path = "/etc/icon.png"
"#;
let err = PluginManifest::from_toml(toml, "hint").unwrap_err();
assert!(err.to_string().contains("icon file path must be relative"));
}
}