Skip to content

Commit

Permalink
wip: extend
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed Nov 22, 2024
1 parent 6f91e6b commit 1840ee1
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions rs/macros/core/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,34 +367,37 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
quote! {}
};

// base v2
let single_base_type = service_args.base_types().len() == 1;

let mut base_expo_types = Vec::with_capacity(service_args.base_types().len());
let mut base_types_funcs = Vec::with_capacity(service_args.base_types().len());
let mut base_types_impl = Vec::with_capacity(service_args.base_types().len());
let mut base_exposure_instantiation = Vec::with_capacity(service_args.base_types().len());
// let mut invocation_dispatches = Vec::with_capacity(service_handlers.len());
let single_base_type = service_args.base_types().len() == 1;

service_args.base_types().iter()
.enumerate()
.for_each(|(idx, base_type)| {
let as_base_ident = format_ident!("as_base_{}", idx);
let base_idx = Index::from(idx);
let base_expo_type = quote! { #sails_path::gstd::services::ServiceExposure< #base_type, () > };

base_expo_types.push(quote! {
#sails_path::gstd::services::ServiceExposure< #base_type, () >
#base_expo_type
});

base_types_funcs.push(quote!{
fn #as_base_ident (&self) -> & #sails_path::gstd::services::ServiceExposure< #base_type, () >;
fn #as_base_ident (&self) -> & #base_expo_type;
});

let extend_ref = if single_base_type {
quote! { &self.extend }
} else {
let base_idx = Index::from(idx);
quote! { &self.extend.#base_idx }
};

base_types_impl.push(quote!{
fn #as_base_ident (&self) -> & #sails_path::gstd::services::ServiceExposure< #base_type, () > {
fn #as_base_ident (&self) -> & #base_expo_type {
#extend_ref
}
});
Expand All @@ -412,6 +415,9 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
};
let base_inst: TokenStream = quote! { ( #( #base_exposure_instantiation ),* ) };

let expo_type =
quote! { #sails_path::gstd::services::ServiceExposure< #service_type_path, #base_type > };

quote!(
#service_impl

Expand All @@ -421,7 +427,7 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
#( #base_types_funcs )*
}

impl #generics #trait_ident #trait_lifetimes for #sails_path::gstd::services::ServiceExposure< #service_type_path, #base_type > #service_type_constraints {
impl #generics #trait_ident #trait_lifetimes for #expo_type #service_type_constraints {
#( #trait_funcs_impl )*

#( #base_types_impl )*
Expand All @@ -439,7 +445,7 @@ fn generate_gservice(args: TokenStream, service_impl: ItemImpl) -> TokenStream {
}

impl #generics #sails_path::gstd::services::Service for #service_type_path #service_type_constraints {
type Exposure = #sails_path::gstd::services::ServiceExposure< #service_type_path, #base_type >;
type Exposure = #expo_type;
type Extend = #base_type;

fn expose(self, #message_id_ident : #sails_path::MessageId, #route_ident : &'static [u8]) -> Self::Exposure {
Expand Down

0 comments on commit 1840ee1

Please sign in to comment.