macro_rules! eval_document {
($ty:ty, $schema:expr, $limits:expr) => {
impl rustev_contract::Document for $ty {
const SCHEMA: &'static str = $schema;
const LIMITS: rustev_contract::bounded::ParseLimits = $limits;
fn schema(&self) -> &str {
&self.schema
}
}
};
($ty:ty, $schema:expr, $limits:expr, $id:ty) => {
$crate::doc::eval_document!($ty, $schema, $limits);
impl rustev_contract::Identified for $ty {
type Id = $id;
fn wrap(digest: String) -> $id {
<$id>::parse(&digest).expect("a tagged digest is an identity")
}
}
};
}
pub(crate) use eval_document;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmittedSchema {
pub file: String,
pub bytes: Vec<u8>,
}
const COMMENT: &str = "Descriptive, derived from the types the bounded parser reads; the parser \
is the only authority on validity. Byte, depth and count bounds are recorded in \
x-rustev-parse-limits (a string length here counts characters, the parser counts bytes), and \
rules the document's own checks apply (ranges, names, declared metrics) are stated in field \
descriptions, not enforced here.";
fn emit<T: schemars::JsonSchema + rustev_contract::Document>() -> EmittedSchema {
let generator = schemars::generate::SchemaSettings::draft2020_12().into_generator();
let root = generator.into_root_schema_for::<T>();
let mut v = serde_json::to_value(&root).expect("a schema is JSON");
v["title"] = T::SCHEMA.into();
v["$comment"] = COMMENT.into();
v.get_mut("properties")
.and_then(|p| p.as_object_mut())
.unwrap_or_else(|| panic!("{}: the derived schema has no properties", T::SCHEMA))
.insert(
"schema".into(),
serde_json::json!({ "type": "string", "const": T::SCHEMA }),
);
let l = T::LIMITS;
v["x-rustev-parse-limits"] = serde_json::json!({
"max_bytes": l.max_bytes,
"max_depth": l.max_depth,
"max_string_bytes": l.max_string_bytes,
"max_collection_len": l.max_collection_len,
"max_total_values": l.max_total_values,
"max_number_bytes": l.max_number_bytes,
"allow_fractional_numbers": l.allow_fractional_numbers,
});
let file = format!("{}.schema.json", T::SCHEMA.replace('/', "-"));
let bytes = rustev_contract::canonical::canonical_value_bytes(&v)
.expect("a schema has canonical bytes");
EmittedSchema { file, bytes }
}
pub fn document_schemas() -> Vec<EmittedSchema> {
let mut out = vec![
emit::<crate::repeat_config::RepeatConfig>(),
emit::<crate::baseline::Baseline>(),
emit::<crate::selection::SelectionGate>(),
emit::<crate::selection::Selection>(),
];
out.sort_by(|a, b| a.file.cmp(&b.file));
out
}