use serde::{Deserialize, Serialize};
pub const PROTOCOL_VERSION: u32 = 1;
pub const PREFIX: &str = "sefy-plugin-";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Manifest {
pub protocol_version: u32,
pub name: String,
pub version: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default)]
pub operations: Vec<Operation>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Operation {
Push,
Pull,
}
impl Operation {
pub fn as_str(self) -> &'static str {
match self {
Self::Push => "push",
Self::Pull => "pull",
}
}
}
impl std::fmt::Display for Operation {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Plugin {
pub executable: String,
pub path: std::path::PathBuf,
pub manifest: Option<Manifest>,
pub usable: bool,
pub reason: Option<String>,
}
impl Plugin {
pub fn name(&self) -> &str {
match &self.manifest {
Some(manifest) => &manifest.name,
None => self
.executable
.strip_prefix(PREFIX)
.unwrap_or(&self.executable),
}
}
pub fn answers_to(&self, name: &str) -> bool {
self.name() == name || self.executable == name
}
pub fn supports(&self, operation: Operation) -> bool {
self.manifest
.as_ref()
.is_some_and(|manifest| manifest.operations.contains(&operation))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_manifest_parses_with_only_its_required_fields() {
let raw = r#"{"protocol_version":1,"name":"demo","version":"0.1.0"}"#;
let manifest: Manifest = serde_json::from_str(raw).unwrap();
assert_eq!(manifest.protocol_version, 1);
assert!(manifest.operations.is_empty());
}
#[test]
fn operations_are_read_from_the_manifest() {
let raw = r#"{"protocol_version":1,"name":"demo","version":"0.1.0",
"operations":["push","pull"]}"#;
let manifest: Manifest = serde_json::from_str(raw).unwrap();
assert_eq!(manifest.operations, vec![Operation::Push, Operation::Pull]);
}
#[test]
fn an_unknown_operation_is_refused_rather_than_guessed() {
let raw = r#"{"protocol_version":1,"name":"demo","version":"0.1.0",
"operations":["teleport"]}"#;
assert!(serde_json::from_str::<Manifest>(raw).is_err());
}
#[test]
fn a_plugin_without_a_manifest_is_still_named() {
let plugin = Plugin {
executable: "sefy-plugin-broken".into(),
path: "/somewhere/sefy-plugin-broken".into(),
manifest: None,
usable: false,
reason: Some("nope".into()),
};
assert_eq!(plugin.name(), "broken");
}
#[test]
fn an_undeclared_operation_is_not_supported() {
let plugin = Plugin {
executable: "sefy-plugin-demo".into(),
path: "/somewhere/sefy-plugin-demo".into(),
manifest: Some(Manifest {
protocol_version: PROTOCOL_VERSION,
name: "demo".into(),
version: "0.1.0".into(),
description: None,
operations: vec![Operation::Push],
}),
usable: true,
reason: None,
};
assert!(plugin.supports(Operation::Push));
assert!(!plugin.supports(Operation::Pull));
}
}