1macro_rules! eval_document {
6 ($ty:ty, $schema:expr, $limits:expr) => {
7 impl rustev_contract::Document for $ty {
8 const SCHEMA: &'static str = $schema;
9 const LIMITS: rustev_contract::bounded::ParseLimits = $limits;
10 fn schema(&self) -> &str {
11 &self.schema
12 }
13 }
14 };
15 ($ty:ty, $schema:expr, $limits:expr, $id:ty) => {
16 $crate::doc::eval_document!($ty, $schema, $limits);
17 impl rustev_contract::Identified for $ty {
18 type Id = $id;
19 fn wrap(digest: String) -> $id {
20 <$id>::parse(&digest).expect("a tagged digest is an identity")
21 }
22 }
23 };
24}
25
26pub(crate) use eval_document;
27
28#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct EmittedSchema {
32 pub file: String,
33 pub bytes: Vec<u8>,
34}
35
36const COMMENT: &str = "Descriptive, derived from the types the bounded parser reads; the parser \
38is the only authority on validity. Byte, depth and count bounds are recorded in \
39x-rustev-parse-limits (a string length here counts characters, the parser counts bytes), and \
40rules the document's own checks apply (ranges, names, declared metrics) are stated in field \
41descriptions, not enforced here.";
42
43fn emit<T: schemars::JsonSchema + rustev_contract::Document>() -> EmittedSchema {
48 let generator = schemars::generate::SchemaSettings::draft2020_12().into_generator();
49 let root = generator.into_root_schema_for::<T>();
50 let mut v = serde_json::to_value(&root).expect("a schema is JSON");
51 v["title"] = T::SCHEMA.into();
52 v["$comment"] = COMMENT.into();
53 v.get_mut("properties")
57 .and_then(|p| p.as_object_mut())
58 .unwrap_or_else(|| panic!("{}: the derived schema has no properties", T::SCHEMA))
59 .insert(
60 "schema".into(),
61 serde_json::json!({ "type": "string", "const": T::SCHEMA }),
62 );
63 let l = T::LIMITS;
64 v["x-rustev-parse-limits"] = serde_json::json!({
65 "max_bytes": l.max_bytes,
66 "max_depth": l.max_depth,
67 "max_string_bytes": l.max_string_bytes,
68 "max_collection_len": l.max_collection_len,
69 "max_total_values": l.max_total_values,
70 "max_number_bytes": l.max_number_bytes,
71 "allow_fractional_numbers": l.allow_fractional_numbers,
72 });
73 let file = format!("{}.schema.json", T::SCHEMA.replace('/', "-"));
74 let bytes = rustev_contract::canonical::canonical_value_bytes(&v)
75 .expect("a schema has canonical bytes");
76 EmittedSchema { file, bytes }
77}
78
79pub fn document_schemas() -> Vec<EmittedSchema> {
85 let mut out = vec![
86 emit::<crate::repeat_config::RepeatConfig>(),
87 emit::<crate::baseline::Baseline>(),
88 emit::<crate::selection::SelectionGate>(),
89 emit::<crate::selection::Selection>(),
90 ];
91 out.sort_by(|a, b| a.file.cmp(&b.file));
92 out
93}