use schemars::schema::{Metadata, Schema, SchemaObject, SubschemaValidation};
use serde_json::json;
pub(crate) fn label_schema(label: &str, schema: Schema) -> Schema {
SchemaObject {
metadata: Some(
Metadata {
title: Some(label.to_string()),
..Default::default()
}
.into(),
),
subschemas: Some(
SubschemaValidation {
all_of: Some(vec![schema]),
..Default::default()
}
.into(),
),
..Default::default()
}
.into()
}
pub(crate) fn extension(
type_name: &str,
version: &str,
) -> schemars::Map<String, serde_json::Value> {
[(
"x-rust-type".to_string(),
json!({
"crate": "oxnet",
"version": version,
"path": format!("oxnet::{}", type_name),
}),
)]
.into_iter()
.collect()
}
#[cfg(test)]
mod tests {
use schemars::gen::SchemaGenerator;
use crate::*;
#[test]
fn test_all_schemas() {
let mut gen = SchemaGenerator::default();
let _ = gen.subschema_for::<IpNet>();
let _ = gen.subschema_for::<Ipv4Net>();
let _ = gen.subschema_for::<Ipv6Net>();
let root = gen.into_root_schema_for::<()>();
expectorate::assert_contents(
"all_schemas.json",
&serde_json::to_string_pretty(&root).expect("json serialization failed"),
);
}
}