Skip to content

Commit

Permalink
fix: manually implement VecM
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Mar 4, 2024
1 parent 3146219 commit 093f9ed
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/curr/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,11 +933,43 @@ impl<T: WriteXdr, const N: usize> WriteXdr for [T; N] {
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)
derive(serde::Serialize, serde::Deserialize)
)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
pub struct VecM<T, const MAX: u32 = { u32::MAX }>(Vec<T>);

impl<T: schemars::JsonSchema, const MAX: u32> schemars::JsonSchema for VecM<T, MAX> {
fn schema_name() -> String {
format!("VecM<{}, {}>", T::schema_name(), MAX)
}

fn is_referenceable() -> bool {
false
}

fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let schema_ = T::json_schema(gen);
let schema = match schema_ {
Schema::Object(SchemaObject {
reference: Some(reference),
..
}) => Schema::new_ref(reference),
_ => schema_,
};
use schemars::schema::*;
SchemaObject {
instance_type: Some(InstanceType::Array.into()),
array: Some(Box::new(ArrayValidation {
items: Some(SingleOrVec::Single(Box::new(schema))),
max_items: Some(MAX),
..Default::default()
})),
..Default::default()
}
.into()
}
}

#[cfg(not(feature = "alloc"))]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
Expand Down
50 changes: 49 additions & 1 deletion tests/serde_tx_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,56 @@
#![cfg(all(feature = "std", feature = "serde"))]

use stellar_xdr::curr as stellar_xdr;
use stellar_xdr::TransactionEnvelope;
use stellar_xdr::{TransactionEnvelope, InvokeHostFunctionOp, SorobanAuthorizationEntry};


#[cfg(feature = "curr")]
#[allow(clippy::too_many_lines, dead_code)]
#[test]
fn vec_m() -> Result<(), Box<dyn std::error::Error>> {
use ::stellar_xdr::curr::VecM;

#[derive(schemars::JsonSchema)]
struct V{a: Vec<u32>}
#[derive(schemars::JsonSchema)]
struct VM{ a: VecM<u32>}

#[derive(schemars::JsonSchema)]
struct V1{v: V, v1: VecM<u32, 12>, v2: VecM<V1, 64>, v3: Vec<u32>}
let schema = schemars::schema_for!(V1);
let schema2 = schemars::schema_for!(V);
let schema3 = schemars::schema_for!(VM);
println!("{schema2:#?}");
println!("{schema3:#?}");
println!("{:#?}", schemars::schema_for!(Vec<u32>));
println!("{:#?}", schemars::schema_for!(VecM<u32>));
let s = serde_json::to_string_pretty(&schema)?;
let s2 = serde_json::to_string_pretty(&schema2)?;
let s3 = serde_json::to_string_pretty(&schema3)?;
// println!("{s}");
// println!("{s2}");
// println!("{s3}");
Ok(())
}

#[cfg(feature = "curr")]
#[allow(clippy::too_many_lines)]
#[test]
fn test_serde_invoke_schema() -> Result<(), Box<dyn std::error::Error>> {
let schema = schemars::schema_for!(InvokeHostFunctionOp);
let s = serde_json::to_string_pretty(&schema)?;
println!("{s}");
Ok(())
}
#[cfg(feature = "curr")]
#[allow(clippy::too_many_lines)]
#[test]
fn test_serde_auth_schema() -> Result<(), Box<dyn std::error::Error>> {
let schema = schemars::schema_for!(SorobanAuthorizationEntry);
let s = serde_json::to_string_pretty(&schema)?;
println!("{s}");
Ok(())
}
#[cfg(feature = "curr")]
#[allow(clippy::too_many_lines)]
#[test]
Expand Down

0 comments on commit 093f9ed

Please sign in to comment.