use std::sync::LazyLock;
use serde_json::json;
use zenkey::schema::{SchemaSet, TypeSchema};
use crate::documents::{
Ack, ActionEvent, ActionOutcome, ActionRequest, DestinationState, DeviceFault, EndpointState,
Identity, IdentityChange, LaneTable, LinkState, Oper, OperChange, RadioState, SecurityState,
Sensor,
};
fn build() -> SchemaSet {
SchemaSet::builder("zenoh-modem")
.json::<u64>("Counter")
.json::<i64>("Gauge")
.json::<f64>("Measure")
.json::<LinkState>("LinkState")
.json::<SecurityState>("SecurityState")
.json::<EndpointState>("EndpointState")
.json::<LaneTable>("LaneTable")
.json::<Oper>("Oper")
.json::<Identity>("Identity")
.json::<RadioState>("RadioState")
.json::<DestinationState>("DestinationState")
.json::<Sensor>("Sensor")
.json::<OperChange>("OperChange")
.json::<DeviceFault>("DeviceFault")
.json::<IdentityChange>("IdentityChange")
.json::<Ack>("Ack")
.json::<ActionRequest>("ActionRequest")
.json::<ActionOutcome>("ActionOutcome")
.json::<ActionEvent>("ActionEvent")
.json::<zenkey::config::ConfigView>("ConfigView")
.json::<zenkey::config::ConfigChange>("ConfigChange")
.json::<zenkey::config::ControlRequest>("ControlRequest")
.json::<zenkey::config::ConfigChangeEvent>("ConfigChangeEvent")
.entry(
"RegistryToml",
TypeSchema::json_schema(json!({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "RegistryToml",
"description": "the producer's registry, verbatim, as TOML text",
"type": "string",
"contentMediaType": "application/toml",
})),
)
.entry(
"SchemaSet",
TypeSchema::json_schema(json!({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "SchemaSet",
"description": "RFC 08 §7's describe envelope",
"type": "object",
"required": ["schema_version", "app", "types"],
"properties": {
"schema_version": { "type": "integer" },
"app": { "type": "string" },
"types": {
"type": "object",
"additionalProperties": {
"type": "object",
"required": ["kind", "hash"],
"properties": {
"kind": { "type": "string" },
"hash": {
"type": "string",
"pattern": "^sha256:[0-9a-f]{64}$"
}
}
}
}
}
})),
)
.build()
}
static SCHEMAS: LazyLock<SchemaSet> = LazyLock::new(build);
static DESCRIBE_JSON: LazyLock<String> = LazyLock::new(|| SCHEMAS.to_json());
pub fn schema_set() -> &'static SchemaSet {
&SCHEMAS
}
pub fn describe_json() -> &'static str {
&DESCRIBE_JSON
}
pub fn verify_total() -> anyhow::Result<()> {
schema_set()
.verify_covers(crate::registry::TYPE_NAMES)
.map_err(|e| anyhow::anyhow!("{e}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_schema_set_is_total() {
verify_total().expect("every registry type must be described");
assert_eq!(schema_set().app(), "zenoh-modem");
}
#[test]
fn every_schema_is_a_valid_json_schema_document() {
for (name, schema) in schema_set().iter() {
let document = schema
.json_document()
.unwrap_or_else(|| panic!("{name} has no JSON document"));
jsonschema::validator_for(document)
.unwrap_or_else(|e| panic!("{name} is not a valid JSON Schema: {e}"));
}
}
#[test]
fn the_describe_reply_parses_back() {
let parsed = SchemaSet::parse(describe_json()).expect("the reply must parse");
assert_eq!(parsed.len(), schema_set().len());
assert_eq!(parsed.app(), "zenoh-modem");
}
}