Skip to content

Commit

Permalink
refactor(schema_derive): reduce duplication when generating the where…
Browse files Browse the repository at this point in the history
… clause

Signed-off-by: ⭐️NINIKA⭐️ <dcnick3@users.noreply.github.com>
  • Loading branch information
DCNick3 committed Oct 7, 2024
1 parent 5f39b82 commit 9969f06
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions crates/iroha_schema_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::parse_quote;

fn override_where_clause(
emitter: &mut Emitter,
where_clause: Option<&syn::WhereClause>,
bounds: Option<&String>,
) -> Option<syn::WhereClause> {
bounds
.and_then(|bounds| emitter.handle(syn::parse_str(&format!("where {bounds}"))))
.unwrap_or(where_clause.cloned())
}

/// Derive [`iroha_schema::TypeId`]
///
/// Check out [`iroha_schema`] documentation
Expand Down Expand Up @@ -230,12 +240,7 @@ fn impl_transparent_into_schema(
) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let name = &input.ident;
let where_clause: Option<syn::WhereClause> = match bounds
.and_then(|bounds| emitter.handle(syn::parse_str(&format!("where {bounds}"))))
{
Some(bounds) => Some(bounds),
None => where_clause.cloned(),
};
let where_clause = override_where_clause(emitter, where_clause, bounds);

quote! {
impl #impl_generics iroha_schema::IntoSchema for #name #ty_generics #where_clause {
Expand Down Expand Up @@ -267,12 +272,7 @@ fn impl_into_schema(
let type_name_body = trait_body(name, &input.generics, false);
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let metadata = metadata(emitter, &input.data);
let where_clause: Option<syn::WhereClause> = match bounds
.and_then(|bounds| emitter.handle(syn::parse_str(&format!("where {bounds}"))))
{
Some(bounds) => Some(bounds),
None => where_clause.cloned(),
};
let where_clause = override_where_clause(emitter, where_clause, bounds);

quote! {
impl #impl_generics iroha_schema::IntoSchema for #name #ty_generics #where_clause {
Expand Down

0 comments on commit 9969f06

Please sign in to comment.