use std::sync::LazyLock;
use serde::Deserialize;
wire_enum! {
#[derive(PartialOrd, Ord)]
pub enum ConformanceProfile {
Core = "KIP-Core",
Schema = "KIP-Schema",
Epistemic = "KIP-Epistemic",
Governance = "KIP-Governance",
Transactions = "KIP-Transactions",
Kql = "KIP-KQL",
Kml = "KIP-KML",
Meta = "KIP-META",
Runtime = "KIP-Runtime",
CognitiveMemory = "KIP-CognitiveMemory",
}
}
impl ConformanceProfile {
pub const fn name(&self) -> &'static str {
self.as_str()
}
pub fn from_name(name: &str) -> Option<Self> {
Self::from_wire(name)
}
}
pub const PROTOCOL_SURFACE: &[ConformanceProfile] = &[
ConformanceProfile::Kql,
ConformanceProfile::Kml,
ConformanceProfile::Meta,
];
static CAPABILITY_NAMES: LazyLock<CapabilityNames> =
LazyLock::new(|| serde_json::from_str(include_str!("../capabilities.json")).unwrap());
#[derive(Deserialize)]
struct CapabilityNames {
registry: Vec<String>,
engine: Vec<String>,
}
pub fn capability_registry_names() -> &'static [String] {
&CAPABILITY_NAMES.registry
}
pub fn capability_engine_names() -> &'static [String] {
&CAPABILITY_NAMES.engine
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_profile_round_trips_by_its_wire_name() {
for profile in ConformanceProfile::ALL {
assert_eq!(
ConformanceProfile::from_name(profile.name()),
Some(*profile)
);
assert_eq!(
serde_json::to_string(profile).unwrap(),
format!("\"{}\"", profile.name())
);
}
assert!(ConformanceProfile::from_name("KIP-Imaginary").is_none());
}
#[test]
fn the_capability_registry_is_the_one_the_specification_prints() {
let spec = include_str!("../SPECIFICATION.md");
let section = spec
.split("## 67.4 Capability registry")
.nth(1)
.expect("§67.4 is in the Specification");
let listing = section
.split("```text")
.nth(1)
.and_then(|rest| rest.split("```").next())
.expect("§67.4 prints the registry in a text block");
let printed: Vec<&str> = listing
.lines()
.filter_map(|line| line.split_whitespace().next())
.collect();
assert_eq!(printed, capability_registry_names());
}
#[test]
fn the_engine_capability_names_are_sorted_and_unique() {
let names = capability_engine_names();
assert!(!names.is_empty());
for pair in names.windows(2) {
assert!(
pair[0] < pair[1],
"{} then {} is out of order",
pair[0],
pair[1]
);
}
}
#[test]
fn the_registry_matches_the_specifications_listing() {
assert_eq!(ConformanceProfile::ALL.len(), 10);
let names: Vec<&str> = ConformanceProfile::ALL.iter().map(|p| p.name()).collect();
assert_eq!(
names,
vec![
"KIP-Core",
"KIP-Schema",
"KIP-Epistemic",
"KIP-Governance",
"KIP-Transactions",
"KIP-KQL",
"KIP-KML",
"KIP-META",
"KIP-Runtime",
"KIP-CognitiveMemory",
]
);
}
}