#[derive(Clone, Debug)]
pub struct RustTypeInfo {
pub crate_name: &'static str,
pub version: &'static str,
pub path: &'static str,
}
pub(crate) const CRATE_NAME: &str = "oxide-update-engine-types";
pub(crate) const VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg(feature = "schemars08")]
pub(crate) const EVENTS_MODULE: &str = "oxide_update_engine_types::events";
pub(crate) const GENERIC_SPEC_PATH: &str =
"oxide_update_engine_types::spec::GenericSpec";
#[cfg(feature = "schemars08")]
pub(crate) fn with_description(
schema: schemars::schema::Schema,
description: &str,
) -> schemars::schema::Schema {
use schemars::schema::{Metadata, Schema, SchemaObject};
match schema {
Schema::Object(obj) if obj.reference.is_some() => {
SchemaObject {
metadata: Some(Box::new(Metadata {
description: Some(description.to_owned()),
..Default::default()
})),
subschemas: Some(Box::new(
schemars::schema::SubschemaValidation {
all_of: Some(vec![Schema::Object(obj)]),
..Default::default()
},
)),
..Default::default()
}
.into()
}
Schema::Object(mut obj) => {
let metadata = obj.metadata.get_or_insert_with(Default::default);
metadata.description = Some(description.to_owned());
obj.into()
}
other => SchemaObject {
metadata: Some(Box::new(Metadata {
description: Some(description.to_owned()),
..Default::default()
})),
subschemas: Some(Box::new(schemars::schema::SubschemaValidation {
all_of: Some(vec![other]),
..Default::default()
})),
..Default::default()
}
.into(),
}
}
#[cfg(feature = "schemars08")]
pub(crate) fn rust_type_for_events(type_path: &str) -> serde_json::Value {
serde_json::json!({
"crate": CRATE_NAME,
"version": VERSION,
"path": type_path,
})
}
#[cfg(feature = "schemars08")]
pub(crate) fn rust_type_for_generic(
info: &RustTypeInfo,
type_name: &str,
) -> serde_json::Value {
serde_json::json!({
"crate": CRATE_NAME,
"version": VERSION,
"path": format!(
"{}::{}",
EVENTS_MODULE, type_name,
),
"parameters": [
{
"x-rust-type": {
"crate": info.crate_name,
"version": info.version,
"path": info.path,
}
}
],
})
}