Skip to content

Commit

Permalink
custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Mar 15, 2024
1 parent bbfea62 commit 017f7d6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 148 deletions.
22 changes: 18 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 $@
Expand All @@ -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 $@
Expand Down
8 changes: 6 additions & 2 deletions src/cli/types/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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)?);
Expand Down
71 changes: 0 additions & 71 deletions src/curr/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<AssetCode4> for [u8; 4] {
#[must_use]
fn from(x: AssetCode4) -> Self {
Expand Down Expand Up @@ -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<AssetCode12> for [u8; 12] {
#[must_use]
fn from(x: AssetCode12) -> Self {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
28 changes: 28 additions & 0 deletions src/curr/jsonschema.rs
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions src/curr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod generated;
pub use generated::*;

mod jsonschema;
mod str;

mod scval_conversions;
Expand Down
Loading

0 comments on commit 017f7d6

Please sign in to comment.