use std::collections::BTreeMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::error::ErrorCode;
use crate::model::validate::{Diagnostics, join};
use crate::model::{SCHEMA_VERSION, StoreModel};
pub const BUILTIN_IDS: [&str; 3] = ["mcp-registry", "anthropic-skills", "skills-sh"];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum CatalogSourceKind {
McpRegistry,
GitTap,
}
impl CatalogSourceKind {
pub const fn as_str(self) -> &'static str {
match self {
CatalogSourceKind::McpRegistry => "mcp_registry",
CatalogSourceKind::GitTap => "git_tap",
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct CatalogRecord {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub kind: Option<CatalogSourceKind>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(rename = "ref", default, skip_serializing_if = "Option::is_none")]
pub git_ref: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
impl CatalogRecord {
pub fn switch(enabled: bool) -> Self {
Self {
kind: None,
url: None,
git_ref: None,
title: None,
enabled: Some(enabled),
extra: toml::Table::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(title = "Surjection catalogs (store/catalogs.toml)")]
pub struct CatalogsFile {
#[schemars(extend("const" = 1))]
pub schema: i64,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub catalogs: BTreeMap<String, CatalogRecord>,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
impl StoreModel for CatalogsFile {
const SCHEMA_NAME: &'static str = "catalogs";
}
impl Default for CatalogsFile {
fn default() -> Self {
Self::new()
}
}
pub fn is_catalog_id(id: &str) -> bool {
(1..=64).contains(&id.len())
&& id.starts_with(|c: char| c.is_ascii_lowercase() || c.is_ascii_digit())
&& id
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(&b))
}
pub fn registry_url_problem(url: &str) -> Option<String> {
let Ok(parsed) = url::Url::parse(url) else {
return Some(format!("{url:?} is not a URL"));
};
let loopback = matches!(parsed.host_str(), Some("localhost" | "127.0.0.1" | "[::1]"));
match parsed.scheme() {
"https" => {}
"http" if loopback => {}
"http" => {
return Some("a registry URL must use https (http only for localhost)".to_owned());
}
_ => return Some(format!("{url:?} is not an https URL")),
}
if parsed.host_str().is_none_or(str::is_empty) {
return Some(format!("{url:?} has no host"));
}
if !parsed.username().is_empty() || parsed.password().is_some() {
return Some("a registry URL must not hold a user name or password".to_owned());
}
if parsed.query().is_some() || parsed.fragment().is_some() {
return Some("a registry URL must not hold a query or fragment".to_owned());
}
None
}
pub fn tap_url_problem(url: &str) -> Option<String> {
let parsed = url::Url::parse(url.trim()).ok()?;
let web = matches!(parsed.scheme(), "http" | "https");
if parsed.password().is_some() || (web && !parsed.username().is_empty()) {
return Some(
"a tap URL must not hold a user name, password or token (it would be saved in your \
store): use your git credential helper instead"
.to_owned(),
);
}
None
}
impl CatalogsFile {
pub fn new() -> Self {
Self {
schema: SCHEMA_VERSION,
catalogs: BTreeMap::new(),
extra: toml::Table::new(),
}
}
pub fn validate(&self) -> Diagnostics {
let mut d = Diagnostics::new();
for (id, record) in &self.catalogs {
let path = join("catalogs", id);
if !is_catalog_id(id) {
d.error(
ErrorCode::InvalidValue,
&path,
format!(
"{id:?} is not a catalog id (1 to 64 characters of a-z, 0-9, '.', '_', '-', \
starting with a letter or digit)"
),
);
continue;
}
if BUILTIN_IDS.contains(&id.as_str()) {
let extra = [
("kind", record.kind.is_some()),
("url", record.url.is_some()),
("ref", record.git_ref.is_some()),
("title", record.title.is_some()),
];
for (key, set) in extra {
if set {
d.error(
ErrorCode::FieldNotAllowed,
join(&path, key),
format!("{id:?} is a built-in catalog: only `enabled` can be set"),
);
}
}
continue;
}
let Some(kind) = record.kind else {
d.error(
ErrorCode::MissingField,
join(&path, "kind"),
"a catalog needs its kind (mcp_registry or git_tap)",
);
continue;
};
let Some(url) = record.url.as_deref().filter(|url| !url.is_empty()) else {
d.error(
ErrorCode::MissingField,
join(&path, "url"),
"a catalog needs its url",
);
continue;
};
match kind {
CatalogSourceKind::McpRegistry => {
if let Some(problem) = registry_url_problem(url) {
d.error(ErrorCode::InvalidValue, join(&path, "url"), problem);
}
if record.git_ref.is_some() {
d.error(
ErrorCode::FieldNotAllowed,
join(&path, "ref"),
"only a git tap has a ref",
);
}
}
CatalogSourceKind::GitTap => {
if let Some(problem) = tap_url_problem(url) {
d.error(ErrorCode::InvalidValue, join(&path, "url"), problem);
} else if let Err(err) = crate::sources::SourceRef::from_git_url(url) {
d.error(ErrorCode::InvalidValue, join(&path, "url"), err.message());
}
if let Some(git_ref) = &record.git_ref
&& let Err(err) = crate::skills::install::check_ref(git_ref)
{
d.error(ErrorCode::InvalidValue, join(&path, "ref"), err.message());
}
}
}
}
d
}
}