From 559a06a7366c4c126a27fb31dd6b641d1e93981c Mon Sep 17 00:00:00 2001 From: Tomasz Kulik Date: Mon, 22 Apr 2024 09:54:00 +0200 Subject: [PATCH] chore: Implement JsonSchema for messages types --- sylvia-derive/src/interfaces.rs | 14 +++++++++ sylvia-derive/src/message.rs | 54 ++++++++++++++------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/sylvia-derive/src/interfaces.rs b/sylvia-derive/src/interfaces.rs index 841cf3aa..3cd104de 100644 --- a/sylvia-derive/src/interfaces.rs +++ b/sylvia-derive/src/interfaces.rs @@ -38,6 +38,20 @@ impl Interfaces { .collect() } + pub fn emit_glue_message_types(&self, msg_ty: &MsgType, contract: &Type) -> Vec { + self.interfaces + .iter() + .map(|interface| { + let ContractMessageAttr { module, .. } = interface; + + let interface_enum = quote! { < #contract as #module ::sv::InterfaceMessagesApi> }; + let type_name = msg_ty.as_accessor_name(); + + quote! { #interface_enum :: #type_name } + }) + .collect() + } + pub fn emit_messages_call(&self, msg_ty: &MsgType) -> Vec { self.interfaces .iter() diff --git a/sylvia-derive/src/message.rs b/sylvia-derive/src/message.rs index e521cd9d..afcde5dd 100644 --- a/sylvia-derive/src/message.rs +++ b/sylvia-derive/src/message.rs @@ -948,15 +948,15 @@ impl<'a> GlueMessage<'a> { }; let contract_enum_name = msg_ty.emit_msg_wrapper_name(); - let enum_name = msg_ty.as_accessor_name(); + let enum_accessor = msg_ty.as_accessor_name(); let contract_name = StripGenerics.fold_type((*contract).clone()); let variants = interfaces.emit_glue_message_variants(msg_ty, contract); + let types = interfaces.emit_glue_message_types(msg_ty, contract); let ep_name = msg_ty.emit_ep_name(); let messages_fn_name = Ident::new(&format!("{}_messages", ep_name), contract.span()); - let contract_variant = - quote! { #contract_name ( <#contract as #sylvia ::types::ContractApi> :: #enum_name ) }; + let contract_variant = quote! { #contract_name ( <#contract as #sylvia ::types::ContractApi> :: #enum_accessor ) }; let mut messages_call = interfaces.emit_messages_call(msg_ty); messages_call.push(quote! { &#messages_fn_name() }); @@ -984,7 +984,7 @@ impl<'a> GlueMessage<'a> { let mut response_schemas_calls = interfaces.emit_response_schemas_calls(msg_ty, contract); response_schemas_calls - .push(quote! {<#contract as #sylvia ::types::ContractApi> :: #enum_name ::response_schemas_impl()}); + .push(quote! {<#contract as #sylvia ::types::ContractApi> :: #enum_accessor ::response_schemas_impl()}); let response_schemas = match msg_ty { MsgType::Query => { @@ -1012,7 +1012,9 @@ impl<'a> GlueMessage<'a> { #contract_variant } - impl #bracketed_wrapper_generics #sylvia ::schemars::JsonSchema for #contract_enum_name #bracketed_wrapper_generics #full_where_clause { + impl #bracketed_wrapper_generics #sylvia ::schemars::JsonSchema + for #contract_enum_name #bracketed_wrapper_generics #full_where_clause { + fn schema_name() -> std::string::String { { let res = format!( @@ -1026,32 +1028,22 @@ impl<'a> GlueMessage<'a> { fn json_schema( gen: &mut #sylvia ::schemars::gen::SchemaGenerator, ) -> #sylvia ::schemars::schema::Schema { - todo!() - // TODO (tkulik): - // schemars::schema::Schema::Object(schemars::schema::SchemaObject { - // subschemas: Some( - // Box::new(schemars::schema::SubschemaValidation { - // any_of: Some( - // <[_]>::into_vec( - // #[rustc_box] - // ::alloc::boxed::Box::new([ - // gen - // .subschema_for::< - // ::Sudo, - // >(), - // gen - // .subschema_for::< - // ::Sudo, - // >(), - // gen.subschema_for::(), - // ]), - // ), - // ), - // ..Default::default() - // }), - // ), - // ..Default::default() - // }) + schemars::schema::Schema::Object(schemars::schema::SchemaObject { + subschemas: Some( + Box::new(schemars::schema::SubschemaValidation { + any_of: Some( + <[_]>::into_vec( + Box::new([ + #(gen.subschema_for::<#types>(),)* + gen.subschema_for::< <#contract as #sylvia ::types::ContractApi> :: #enum_accessor >(), + ]), + ), + ), + ..Default::default() + }), + ), + ..Default::default() + }) } }