use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
pub const VIEWER_DIR: &str = "viewer";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.Manifest")]
pub struct Manifest {
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub mcp: Option<Mcp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub viewer: Option<Viewer>,
}
impl Manifest {
pub fn validate(&self) -> Result<(), String> {
if self.mcp.is_none() && self.viewer.is_none() {
return Err(
"plugin manifest declares neither `mcp` nor `viewer` — it would do nothing"
.to_string(),
);
}
if let Some(mcp) = &self.mcp {
mcp.validate()?;
}
if let Some(viewer) = &self.viewer {
viewer.validate()?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.Mcp")]
pub struct Mcp {
pub containerfile: String,
pub port: u16,
pub postgres: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub development: Option<McpDevelopment>,
}
impl Mcp {
pub fn validate(&self) -> Result<(), String> {
if let Some(development) = &self.development {
development.validate()?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.McpDevelopment")]
pub struct McpDevelopment {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[schemars(extend("omitempty" = true))]
pub caches: Vec<String>,
}
impl McpDevelopment {
pub fn validate(&self) -> Result<(), String> {
for (index, cache) in self.caches.iter().enumerate() {
let at = format!("`mcp.development.caches[{index}]`");
if cache.is_empty() {
return Err(format!("plugin manifest: {at} is empty"));
}
if !cache.starts_with('/') {
return Err(format!(
"plugin manifest: {at} must be an absolute container path \
(starting with `/`), got {cache:?}"
));
}
if cache.contains('\\') {
return Err(format!(
"plugin manifest: {at} must use forward slashes — it names a \
path INSIDE the container, got {cache:?}"
));
}
if cache == "/" {
return Err(format!(
"plugin manifest: {at} cannot be `/` — mounting the whole \
root would hide the image"
));
}
if cache
.trim_end_matches('/')
.split('/')
.skip(1)
.any(|part| part.is_empty() || part == "." || part == "..")
{
return Err(format!(
"plugin manifest: {at} must not contain empty, `.` or `..` \
components, got {cache:?}"
));
}
}
let mut seen = std::collections::BTreeSet::new();
for cache in &self.caches {
if !seen.insert(cache.trim_end_matches('/')) {
return Err(format!(
"plugin manifest: `mcp.development.caches` names {cache:?} \
more than once"
));
}
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.Viewer")]
pub struct Viewer {
pub containerfile: String,
pub output: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub icon: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub tabs: Option<Vec<ViewerTab>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub scripts: Option<Vec<ViewerScript>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub development: Option<ViewerDevelopment>,
}
impl Viewer {
pub fn validate(&self) -> Result<(), String> {
if let Some(development) = &self.development {
development.validate()?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.ViewerDevelopment")]
pub struct ViewerDevelopment {
pub output: String,
}
impl ViewerDevelopment {
pub fn validate(&self) -> Result<(), String> {
let at = "`viewer.development.output`";
if self.output.is_empty() {
return Err(format!("plugin manifest: {at} is empty"));
}
if self.output.starts_with('/') {
return Err(format!(
"plugin manifest: {at} must be repo-relative (no leading `/`), \
got {:?}",
self.output
));
}
if self.output.contains('\\') {
return Err(format!(
"plugin manifest: {at} must use forward slashes, got {:?}",
self.output
));
}
if self
.output
.trim_end_matches('/')
.split('/')
.any(|part| part.is_empty() || part == "." || part == "..")
{
return Err(format!(
"plugin manifest: {at} must not contain empty, `.` or `..` \
components, got {:?}",
self.output
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.ViewerScript")]
pub struct ViewerScript {
pub name: String,
pub module: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[schemars(rename = "cli.plugins.ViewerTab")]
#[serde(untagged)]
pub enum ViewerTab {
#[schemars(title = "Channel")]
Channel {
channel_key: String,
module: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
export: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
styles: Option<Vec<String>>,
},
#[schemars(title = "Tab")]
Tab {
title: String,
module: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
export: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
styles: Option<Vec<String>>,
},
}