From 017f7d6f3060f79c51c0af3dd503f317182500e7 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 15 Mar 2024 20:50:54 +1000 Subject: [PATCH] custom types --- Makefile | 22 ++++++++++--- src/cli/types/schema.rs | 8 +++-- src/curr/generated.rs | 71 ----------------------------------------- src/curr/jsonschema.rs | 28 ++++++++++++++++ src/curr/mod.rs | 1 + src/next/generated.rs | 71 ----------------------------------------- src/next/jsonschema.rs | 28 ++++++++++++++++ src/next/mod.rs | 1 + 8 files changed, 82 insertions(+), 148 deletions(-) create mode 100644 src/curr/jsonschema.rs create mode 100644 src/next/jsonschema.rs diff --git a/Makefile b/Makefile index 16df6c2a..bbfb2b3d 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ XDRGEN_VERSION=4ea81a540e25623567badd1f7dd649cc46cf9f21 # XDRGEN_LOCAL=1 XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12 XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12 +XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12 +XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12 all: build test @@ -39,12 +41,18 @@ ifeq ($(XDRGEN_LOCAL),) docker run -i --rm -v $$PWD:/wd -w /wd docker.io/library/ruby:latest /bin/bash -c '\ gem install specific_install -v 0.3.8 && \ gem specific_install https://github.com/stellar/xdrgen.git -b $(XDRGEN_VERSION) && \ - xdrgen --language rust --namespace generated --output src/curr --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR) $^ \ + xdrgen --language rust --namespace generated --output src/curr \ + --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR) \ + --rust-types-custom-jsonschema-impl '$(XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_CURR)' \ + $^ \ ' else docker run -i --rm -v $$PWD/../xdrgen:/xdrgen -v $$PWD:/wd -w /wd docker.io/library/ruby:latest /bin/bash -c '\ pushd /xdrgen && bundle install --deployment && rake install && popd && \ - xdrgen --language rust --namespace generated --output src/curr --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR) $^ \ + xdrgen --language rust --namespace generated --output src/curr \ + --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR) \ + --rust-types-custom-jsonschema-impl '$(XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_CURR)' \ + $^ \ ' endif rustfmt $@ @@ -58,12 +66,18 @@ ifeq ($(XDRGEN_LOCAL),) docker run -i --rm -v $$PWD:/wd -w /wd docker.io/library/ruby:latest /bin/bash -c '\ gem install specific_install -v 0.3.8 && \ gem specific_install https://github.com/stellar/xdrgen.git -b $(XDRGEN_VERSION) && \ - xdrgen --language rust --namespace generated --output src/next --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT) $^ \ + xdrgen --language rust --namespace generated --output src/next \ + --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT) \ + --rust-types-custom-jsonschema-impl '$(XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_NEXT)' \ + $^ \ ' else docker run -i --rm -v $$PWD/../xdrgen:/xdrgen -v $$PWD:/wd -w /wd docker.io/library/ruby:latest /bin/bash -c '\ pushd /xdrgen && bundle install --deployment && rake install && popd && \ - xdrgen --language rust --namespace generated --output src/next --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT) $^ \ + xdrgen --language rust --namespace generated --output src/next \ + --rust-types-custom-str-impl $(XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT) \ + --rust-types-custom-jsonschema-impl '$(XDRGEN_TYPES_CUSTOM_JSONSCHEMA_IMPL_NEXT)' \ + $^ \ ' endif rustfmt $@ diff --git a/src/cli/types/schema.rs b/src/cli/types/schema.rs index ef5c7bc8..730ca6c0 100644 --- a/src/cli/types/schema.rs +++ b/src/cli/types/schema.rs @@ -26,11 +26,12 @@ pub struct Cmd { #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)] pub enum OutputFormat { JsonSchemaDraft7, + JsonSchemaDraft201909, } impl Default for OutputFormat { fn default() -> Self { - Self::JsonSchemaDraft7 + Self::JsonSchemaDraft201909 } } @@ -41,7 +42,10 @@ macro_rules! run_x { let r#type = crate::$m::TypeVariant::from_str(&self.r#type).map_err(|_| { Error::UnknownType(self.r#type.clone(), &crate::$m::TypeVariant::VARIANTS_STR) })?; - let settings = SchemaSettings::draft07(); + let settings = match self.output { + OutputFormat::JsonSchemaDraft7 => SchemaSettings::draft07(), + OutputFormat::JsonSchemaDraft201909 => SchemaSettings::draft2019_09(), + }; let generator = settings.into_generator(); let schema = r#type.json_schema(generator); println!("{}", serde_json::to_string_pretty(&schema)?); diff --git a/src/curr/generated.rs b/src/curr/generated.rs index c5cc787b..86f1adaa 100644 --- a/src/curr/generated.rs +++ b/src/curr/generated.rs @@ -8718,7 +8718,6 @@ impl WriteXdr for ScAddressType { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum ScAddress { Account(AccountId), @@ -10753,37 +10752,6 @@ impl core::fmt::Debug for AssetCode4 { Ok(()) } } -#[cfg(feature = "schemars")] -impl schemars::JsonSchema for AssetCode4 { - fn schema_name() -> String { - "AssetCode4".to_string() - } - - fn is_referenceable() -> bool { - false - } - - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - let schema_ = String::json_schema(gen); - if let schemars::schema::Schema::Object(mut schema) = schema_ { - schema.extensions.insert( - "contentEncoding".to_owned(), - serde_json::Value::String("hex".to_string()), - ); - schema.extensions.insert( - "contentMediaType".to_owned(), - serde_json::Value::String("application/binary".to_string()), - ); - mut_string(schema.into(), |string| schemars::schema::StringValidation { - max_length: 4_u32.checked_mul(2).map(Some).unwrap_or_default(), - min_length: 4_u32.checked_mul(2).map(Some).unwrap_or_default(), - ..string - }) - } else { - schema_ - } - } -} impl From for [u8; 4] { #[must_use] fn from(x: AssetCode4) -> Self { @@ -10885,37 +10853,6 @@ impl core::fmt::Debug for AssetCode12 { Ok(()) } } -#[cfg(feature = "schemars")] -impl schemars::JsonSchema for AssetCode12 { - fn schema_name() -> String { - "AssetCode12".to_string() - } - - fn is_referenceable() -> bool { - false - } - - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - let schema_ = String::json_schema(gen); - if let schemars::schema::Schema::Object(mut schema) = schema_ { - schema.extensions.insert( - "contentEncoding".to_owned(), - serde_json::Value::String("hex".to_string()), - ); - schema.extensions.insert( - "contentMediaType".to_owned(), - serde_json::Value::String("application/binary".to_string()), - ); - mut_string(schema.into(), |string| schemars::schema::StringValidation { - max_length: 12_u32.checked_mul(2).map(Some).unwrap_or_default(), - min_length: 12_u32.checked_mul(2).map(Some).unwrap_or_default(), - ..string - }) - } else { - schema_ - } - } -} impl From for [u8; 12] { #[must_use] fn from(x: AssetCode12) -> Self { @@ -11134,7 +11071,6 @@ impl WriteXdr for AssetType { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum AssetCode { CreditAlphanum4(AssetCode4), @@ -24551,7 +24487,6 @@ impl WriteXdr for LiquidityPoolParameters { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct MuxedAccountMed25519 { pub id: u64, pub ed25519: Uint256, @@ -24603,7 +24538,6 @@ impl WriteXdr for MuxedAccountMed25519 { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum MuxedAccount { Ed25519(Uint256), @@ -42012,7 +41946,6 @@ impl WriteXdr for SignerKeyType { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum PublicKey { PublicKeyTypeEd25519(Uint256), @@ -42115,7 +42048,6 @@ impl WriteXdr for PublicKey { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct SignerKeyEd25519SignedPayload { pub ed25519: Uint256, pub payload: BytesM<64>, @@ -42175,7 +42107,6 @@ impl WriteXdr for SignerKeyEd25519SignedPayload { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum SignerKey { Ed25519(Uint256), @@ -42548,7 +42479,6 @@ impl AsRef<[u8]> for SignatureHint { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[derive(Debug)] pub struct NodeId(pub PublicKey); @@ -42603,7 +42533,6 @@ impl WriteXdr for NodeId { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[derive(Debug)] pub struct AccountId(pub PublicKey); diff --git a/src/curr/jsonschema.rs b/src/curr/jsonschema.rs new file mode 100644 index 00000000..ce23b940 --- /dev/null +++ b/src/curr/jsonschema.rs @@ -0,0 +1,28 @@ +#![cfg(feature = "schemars")] +use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema}; + +macro_rules! impl_json_schema_string { + ($type:ident) => { + impl JsonSchema for super::$type { + fn schema_name() -> String { + stringify!($type).to_string() + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + String::json_schema(gen) + } + } + }; +} + +impl_json_schema_string!(PublicKey); +impl_json_schema_string!(AccountId); +impl_json_schema_string!(MuxedAccount); +impl_json_schema_string!(MuxedAccountMed25519); +impl_json_schema_string!(SignerKey); +impl_json_schema_string!(SignerKeyEd25519SignedPayload); +impl_json_schema_string!(NodeId); +impl_json_schema_string!(ScAddress); +impl_json_schema_string!(AssetCode); +impl_json_schema_string!(AssetCode4); +impl_json_schema_string!(AssetCode12); diff --git a/src/curr/mod.rs b/src/curr/mod.rs index 288c1e2e..ec2f1e37 100644 --- a/src/curr/mod.rs +++ b/src/curr/mod.rs @@ -1,6 +1,7 @@ mod generated; pub use generated::*; +mod jsonschema; mod str; mod scval_conversions; diff --git a/src/next/generated.rs b/src/next/generated.rs index 3c7a959a..3d4048dc 100644 --- a/src/next/generated.rs +++ b/src/next/generated.rs @@ -8718,7 +8718,6 @@ impl WriteXdr for ScAddressType { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum ScAddress { Account(AccountId), @@ -10753,37 +10752,6 @@ impl core::fmt::Debug for AssetCode4 { Ok(()) } } -#[cfg(feature = "schemars")] -impl schemars::JsonSchema for AssetCode4 { - fn schema_name() -> String { - "AssetCode4".to_string() - } - - fn is_referenceable() -> bool { - false - } - - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - let schema_ = String::json_schema(gen); - if let schemars::schema::Schema::Object(mut schema) = schema_ { - schema.extensions.insert( - "contentEncoding".to_owned(), - serde_json::Value::String("hex".to_string()), - ); - schema.extensions.insert( - "contentMediaType".to_owned(), - serde_json::Value::String("application/binary".to_string()), - ); - mut_string(schema.into(), |string| schemars::schema::StringValidation { - max_length: 4_u32.checked_mul(2).map(Some).unwrap_or_default(), - min_length: 4_u32.checked_mul(2).map(Some).unwrap_or_default(), - ..string - }) - } else { - schema_ - } - } -} impl From for [u8; 4] { #[must_use] fn from(x: AssetCode4) -> Self { @@ -10885,37 +10853,6 @@ impl core::fmt::Debug for AssetCode12 { Ok(()) } } -#[cfg(feature = "schemars")] -impl schemars::JsonSchema for AssetCode12 { - fn schema_name() -> String { - "AssetCode12".to_string() - } - - fn is_referenceable() -> bool { - false - } - - fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - let schema_ = String::json_schema(gen); - if let schemars::schema::Schema::Object(mut schema) = schema_ { - schema.extensions.insert( - "contentEncoding".to_owned(), - serde_json::Value::String("hex".to_string()), - ); - schema.extensions.insert( - "contentMediaType".to_owned(), - serde_json::Value::String("application/binary".to_string()), - ); - mut_string(schema.into(), |string| schemars::schema::StringValidation { - max_length: 12_u32.checked_mul(2).map(Some).unwrap_or_default(), - min_length: 12_u32.checked_mul(2).map(Some).unwrap_or_default(), - ..string - }) - } else { - schema_ - } - } -} impl From for [u8; 12] { #[must_use] fn from(x: AssetCode12) -> Self { @@ -11134,7 +11071,6 @@ impl WriteXdr for AssetType { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum AssetCode { CreditAlphanum4(AssetCode4), @@ -24551,7 +24487,6 @@ impl WriteXdr for LiquidityPoolParameters { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct MuxedAccountMed25519 { pub id: u64, pub ed25519: Uint256, @@ -24603,7 +24538,6 @@ impl WriteXdr for MuxedAccountMed25519 { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum MuxedAccount { Ed25519(Uint256), @@ -42012,7 +41946,6 @@ impl WriteXdr for SignerKeyType { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum PublicKey { PublicKeyTypeEd25519(Uint256), @@ -42115,7 +42048,6 @@ impl WriteXdr for PublicKey { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct SignerKeyEd25519SignedPayload { pub ed25519: Uint256, pub payload: BytesM<64>, @@ -42175,7 +42107,6 @@ impl WriteXdr for SignerKeyEd25519SignedPayload { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[allow(clippy::large_enum_variant)] pub enum SignerKey { Ed25519(Uint256), @@ -42548,7 +42479,6 @@ impl AsRef<[u8]> for SignatureHint { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[derive(Debug)] pub struct NodeId(pub PublicKey); @@ -42603,7 +42533,6 @@ impl WriteXdr for NodeId { all(feature = "serde", feature = "alloc"), derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr) )] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[derive(Debug)] pub struct AccountId(pub PublicKey); diff --git a/src/next/jsonschema.rs b/src/next/jsonschema.rs new file mode 100644 index 00000000..ce23b940 --- /dev/null +++ b/src/next/jsonschema.rs @@ -0,0 +1,28 @@ +#![cfg(feature = "schemars")] +use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema}; + +macro_rules! impl_json_schema_string { + ($type:ident) => { + impl JsonSchema for super::$type { + fn schema_name() -> String { + stringify!($type).to_string() + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + String::json_schema(gen) + } + } + }; +} + +impl_json_schema_string!(PublicKey); +impl_json_schema_string!(AccountId); +impl_json_schema_string!(MuxedAccount); +impl_json_schema_string!(MuxedAccountMed25519); +impl_json_schema_string!(SignerKey); +impl_json_schema_string!(SignerKeyEd25519SignedPayload); +impl_json_schema_string!(NodeId); +impl_json_schema_string!(ScAddress); +impl_json_schema_string!(AssetCode); +impl_json_schema_string!(AssetCode4); +impl_json_schema_string!(AssetCode12); diff --git a/src/next/mod.rs b/src/next/mod.rs index 288c1e2e..ec2f1e37 100644 --- a/src/next/mod.rs +++ b/src/next/mod.rs @@ -1,6 +1,7 @@ mod generated; pub use generated::*; +mod jsonschema; mod str; mod scval_conversions;