use std::collections::{BTreeMap, BTreeSet};
use std::sync::LazyLock;
use serde::Deserialize;
use serde_json::Value;
use crate::pattern::Pattern;
pub const REGISTRY_JSON: &str = include_str!("registry.json");
#[derive(Clone, Debug, Default, Deserialize)]
pub struct AttrSpec {
#[serde(rename = "type", default)]
pub attr_type: String,
#[serde(default, rename = "enum")]
pub enum_values: Option<Vec<String>>,
#[serde(default)]
pub pattern: String,
#[serde(default)]
pub minimum: Option<f64>,
#[serde(default)]
pub maximum: Option<f64>,
#[serde(default)]
pub items: Option<Box<AttrSpec>>,
#[serde(skip)]
pub(crate) compiled: Option<Pattern>,
}
#[derive(Clone, Debug, Default, Deserialize)]
pub struct KindSpec {
pub body: String,
pub forms: Vec<String>,
#[serde(default)]
pub attributes: BTreeMap<String, AttrSpec>,
#[serde(default)]
pub required: Vec<String>,
#[serde(default)]
pub family: String,
#[serde(default)]
pub alias: Option<String>,
#[serde(default)]
pub flags: Option<BTreeMap<String, Value>>,
#[serde(default)]
pub parent: Option<String>,
#[serde(default)]
pub opaque: bool,
#[serde(default)]
pub formats: Vec<String>,
}
impl KindSpec {
pub fn takes(&self, form: &str) -> bool {
self.forms.iter().any(|f| f == form)
}
pub fn formats(&self) -> Vec<String> {
if !self.formats.is_empty() {
return self.formats.clone();
}
vec![if self.body == "yaml" { "yaml".into() } else { "markdown".into() }]
}
}
#[derive(Debug, Default, Deserialize)]
struct Grammar {
kind: String,
id: String,
parameter: String,
digest: String,
}
#[derive(Debug, Default, Deserialize)]
pub struct Registry {
pub format: String,
pub spec: String,
pub common_attributes: BTreeMap<String, AttrSpec>,
pub kinds: BTreeMap<String, KindSpec>,
pub inline: BTreeMap<String, BTreeMap<String, AttrSpec>>,
pub keywords: BTreeMap<String, String>,
pub keyword_flags: BTreeMap<String, BTreeMap<String, Value>>,
grammar: Grammar,
#[serde(skip)]
kind_pattern: Option<Pattern>,
#[serde(skip)]
id_pattern: Option<Pattern>,
#[serde(skip)]
parameter_pattern: Option<Pattern>,
#[serde(skip)]
digest_pattern: Option<Pattern>,
}
static REGISTRY: LazyLock<Registry> = LazyLock::new(|| {
let mut r: Registry = serde_json::from_str(REGISTRY_JSON).unwrap_or_default();
let compile = |specs: &mut BTreeMap<String, AttrSpec>| {
for s in specs.values_mut() {
if !s.pattern.is_empty() {
s.compiled = Some(Pattern::must(&s.pattern));
}
}
};
compile(&mut r.common_attributes);
for k in r.kinds.values_mut() {
compile(&mut k.attributes);
}
for a in r.inline.values_mut() {
compile(a);
}
r.kind_pattern = Some(Pattern::must(&r.grammar.kind));
r.id_pattern = Some(Pattern::must(&r.grammar.id));
r.parameter_pattern = Some(Pattern::must(&r.grammar.parameter));
r.digest_pattern = Some(Pattern::must(&r.grammar.digest));
r
});
pub fn registry() -> &'static Registry {
®ISTRY
}
static NEVER: LazyLock<Pattern> = LazyLock::new(|| Pattern::must("[^\\x{0}-\\x{10ffff}]"));
impl Registry {
pub fn kind(&self, name: &str) -> Option<&KindSpec> {
self.kinds.get(name)
}
pub fn kind_pattern(&self) -> &Pattern {
self.kind_pattern.as_ref().unwrap_or(&NEVER)
}
pub fn id_pattern(&self) -> &Pattern {
self.id_pattern.as_ref().unwrap_or(&NEVER)
}
pub fn parameter_pattern(&self) -> &Pattern {
self.parameter_pattern.as_ref().unwrap_or(&NEVER)
}
pub fn digest_pattern(&self) -> &Pattern {
self.digest_pattern.as_ref().unwrap_or(&NEVER)
}
}
pub fn kind(name: &str) -> Option<&'static KindSpec> {
registry().kinds.get(name)
}
pub const RULES: &[&str] = &["must", "should", "may", "never", "guardrail", "always", "avoid"];
pub fn is_rule(kind: &str) -> bool {
RULES.contains(&kind)
}
pub const TYPED_REFERENCES: &[&str] = &["person", "role", "skill", "mcp", "tool", "agent", "service", "source", "resource"];
pub(crate) fn typed_reference(kind: &str) -> bool {
TYPED_REFERENCES.contains(&kind)
}
pub fn inline_required(kind: &str) -> Option<&'static str> {
Some(match kind {
"ref" | "person" | "role" | "skill" | "mcp" | "tool" | "agent" | "service" | "source" | "resource" | "cite" => "ref",
"button" => "href",
"abbr" => "title",
_ => return None,
})
}
pub const FRONT_MATTER_KEYS: &[&str] = &[
"spec",
"id",
"version",
"title",
"description",
"language",
"tags",
"license",
"parameters",
"extends",
"extensions",
"references",
"resolvers",
"exports",
"presentation",
"requires",
"integrations",
"host-integrations",
];
pub const BACKMATTER_KEYS: &[&str] = &["authors", "owners", "review", "history", "sources", "approvals", "notes", "test_results"];
pub const PRESENTATION_ATTRS: &[&str] = &["class", "style", "count", "columns", "span", "open"];
pub fn canonical_kind(kind: &str) -> &str {
match self::kind(kind).and_then(|k| k.alias.as_deref()) {
Some(alias) => alias,
None => kind,
}
}
pub fn attr_spec(kind: &str, key: &str) -> Option<&'static AttrSpec> {
self::kind(kind).and_then(|k| k.attributes.get(key)).or_else(|| registry().common_attributes.get(key))
}
pub fn inline_attr_spec(kind: &str, key: &str) -> Option<&'static AttrSpec> {
if let Some(s) = registry().inline.get(kind).and_then(|a| a.get(key)) {
return Some(s);
}
if matches!(key, "class" | "style" | "id") {
return registry().common_attributes.get(key);
}
None
}
pub fn all_families() -> BTreeSet<String> {
registry().kinds.values().map(|k| k.family.clone()).filter(|f| !f.is_empty()).collect()
}
pub(crate) fn keyword_names() -> String {
let mut names: Vec<&String> = registry().keywords.keys().collect();
names.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b)));
names.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("|")
}