kitchen_tools/plugin/
mod.rs1pub mod client;
18pub mod discover;
19pub mod manager;
20pub mod protocol;
21
22pub use client::PluginProcess;
23pub use discover::PluginKind;
24pub use manager::{Installed, Manager};
25pub use protocol::{CoreRequest, Event, PROTOCOL};
26
27use serde::{Deserialize, Serialize};
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct RuntimeStatus {
32 pub ready: bool,
34 pub detail: String,
36}
37
38fn protocol_one() -> u32 {
39 1
40}
41
42#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44pub struct PluginInfo {
45 pub name: String,
47 pub version: String,
49 #[serde(default = "protocol_one")]
51 pub protocol: u32,
52 #[serde(default)]
54 pub licence: String,
55 #[serde(default)]
57 pub capabilities: Vec<String>,
58 #[serde(default)]
60 pub runtime: Option<RuntimeStatus>,
61 #[serde(default)]
63 pub program: String,
64 #[serde(flatten)]
66 pub extra: serde_json::Map<String, serde_json::Value>,
67}
68
69impl PluginInfo {
70 pub fn compatible(&self) -> Result<(), String> {
72 match self.protocol {
73 PROTOCOL => Ok(()),
74 p if p > PROTOCOL => {
75 Err(format!("{} speaks protocol {p}; this app speaks {PROTOCOL}: update the app", self.name))
76 }
77 p => Err(format!("{} speaks protocol {p}; this app speaks {PROTOCOL}: update the plugin", self.name)),
78 }
79 }
80
81 pub fn can(&self, capability: &str) -> bool {
83 self.capabilities.iter().any(|c| c == capability)
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn manifests_keep_the_apps_fields_and_check_the_protocol() {
93 let info: PluginInfo = serde_json::from_str(r#"{"name":"lumen-ml-sam","version":"0.1.0","licence":"Apache-2.0","capabilities":["segment"],"models":[{"id":"sam2-tiny"}]}"#).unwrap();
94 assert_eq!(info.protocol, 1, "a plugin that does not say speaks 1");
95 assert!(info.compatible().is_ok() && info.can("segment") && !info.can("sort"));
96 assert_eq!(info.extra["models"][0]["id"], "sam2-tiny");
97 let newer = PluginInfo { protocol: 2, ..info.clone() };
98 assert!(newer.compatible().unwrap_err().contains("update the app"));
99 let back: PluginInfo = serde_json::from_str(&serde_json::to_string(&info).unwrap()).unwrap();
100 assert_eq!(back, info);
101 }
102}