Skip to content

Commit

Permalink
Update the uuid kinds to emit a JSON Schema that includes the
Browse files Browse the repository at this point in the history
x-rust-type extension
  • Loading branch information
ahl committed May 11, 2024
1 parent 572f621 commit 5842ade
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion uuid-kinds/Cargo.toml
Expand Up @@ -14,11 +14,12 @@ workspace = true
[dependencies]
newtype-uuid.workspace = true
schemars = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
paste.workspace = true

[features]
default = ["std"]
serde = ["newtype-uuid/serde"]
schemars08 = ["newtype-uuid/schemars08", "schemars"]
schemars08 = ["newtype-uuid/schemars08", "dep:schemars", "dep:serde_json"]
std = ["newtype-uuid/std"]
uuid-v4 = ["newtype-uuid/v4"]
58 changes: 53 additions & 5 deletions uuid-kinds/src/lib.rs
Expand Up @@ -14,15 +14,63 @@ pub use newtype_uuid::{
GenericUuid, ParseError, TagError, TypedUuid, TypedUuidKind, TypedUuidTag,
};

#[cfg(feature = "schemars08")]
use schemars::JsonSchema;

macro_rules! impl_typed_uuid_kind {
($($kind:ident => $tag:literal),* $(,)?) => {
$(
paste::paste! {
#[cfg_attr(feature = "schemars08", derive(JsonSchema))]
pub enum [< $kind Kind>] {}
pub enum [< $kind Kind >] {}

#[cfg(feature = "schemars08")]
impl schemars::JsonSchema for [< $kind Kind >]{
fn schema_name() -> String {
stringify!([< $kind Kind >]).to_string()
}

fn is_referenceable() -> bool {
false
}

fn json_schema(_: &mut schemars::gen::SchemaGenerator)
-> schemars::schema::Schema
{
use schemars::schema::Metadata;
use schemars::schema::Schema;
use schemars::schema::SchemaObject;
use schemars::schema::SubschemaValidation;

SchemaObject {
metadata: Some(
Metadata{
title: Some(Self::schema_name()),
..Default::default()
}.into(),
),
subschemas: Some(
SubschemaValidation {
not: Some(Schema::Bool(true).into()),
..Default::default()
}
.into(),
),
extensions: [(
"x-rust-type".to_string(),
serde_json::json!({
"crate": "omicron-uuid-kinds",
"version": "0.1.0",
"path":
format!(
"omicron_uuid_kinds::{}",
Self::schema_name(),
),
}),
)]
.into_iter()
.collect(),
..Default::default()
}
.into()
}
}

impl TypedUuidKind for [< $kind Kind >] {
#[inline]
Expand Down

0 comments on commit 5842ade

Please sign in to comment.