use std::sync::LazyLock;
use dry::macro_for;
use jsonschema::{Retrieve, Uri};
use log::debug;
use serde_json::Value;
use crate::types::Map;
#[derive(Debug)]
pub(crate) struct InMemoryRetriever {
schemas: Map<String, Value>,
}
impl Retrieve for &InMemoryRetriever {
fn retrieve(
&self,
uri: &Uri<String>,
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
self.schemas
.get(uri.as_str())
.cloned()
.ok_or_else(|| format!("Schema not found: {uri}").into())
}
}
pub(crate) static JSON_SCHEMA_RETRIEVER: LazyLock<InMemoryRetriever> = LazyLock::new(|| {
debug!("Loading the JSON Schemas from assets.");
let mut schemas = Map::default();
macro_for!(
$schema in [
catcpd, catbn,
gausscpd, gaussbn,
catcim, catctbn,
digraph, ungraph
] {
let schema = include_str!(concat!(stringify!($schema), ".schema.json"));
if let Ok(schema) = serde_json::from_str::<Value>(schema) {
let id = concat!("json-schema:///", stringify!($schema), ".schema.json");
schemas.insert(id.to_owned(), schema);
}
});
InMemoryRetriever { schemas }
});