Skip to content

Commit

Permalink
fix macros core integration tests & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed May 16, 2024
1 parent f23fac0 commit e7f4fd6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions macros/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
//! Implemntation of the procedural macros exposed via the `sails-macros` crate.
pub use program::gprogram;
pub use program::gprogram_safe;
pub use route::groute;
pub use service::gservice;
pub use service::gservice_safe;

mod program;
mod route;
Expand Down
20 changes: 18 additions & 2 deletions macros/core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,38 @@ use syn::{
Type, TypePath, Visibility,
};

pub fn gprogram_safe(program_impl_tokens: TokenStream2) -> TokenStream2 {
let program_impl = parse_program_impl(program_impl_tokens);
check_program_single(&program_impl);
gen_gprogram_impl(program_impl)
}

pub fn gprogram(program_impl_tokens: TokenStream2) -> TokenStream2 {
let program_impl: ItemImpl = syn::parse2(program_impl_tokens).unwrap_or_else(|err| {
let program_impl = parse_program_impl(program_impl_tokens);
gen_gprogram_impl(program_impl)
}

fn parse_program_impl(program_impl_tokens: TokenStream2) -> ItemImpl {
syn::parse2(program_impl_tokens).unwrap_or_else(|err| {
abort!(
err.span(),
"`gprogram` attribute can be applied to impls only: {}",
err
)
});
})
}

fn check_program_single(program_impl: &ItemImpl) {
if unsafe { shared::PROGRAM_SPAN }.is_some() {
abort!(
program_impl.span(),
"multiple `gprogram` attributes are not allowed"
)
}
unsafe { shared::PROGRAM_SPAN = Some(program_impl.span()) };
}

fn gen_gprogram_impl(program_impl: ItemImpl) -> TokenStream2 {
let services_ctors = discover_services_ctors(&program_impl);

let mut program_impl = program_impl.clone();
Expand Down
21 changes: 18 additions & 3 deletions macros/core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,29 @@ use syn::{
TypeParamBound, Visibility, WhereClause, WherePredicate,
};

pub fn gservice_safe(service_impl_tokens: TokenStream2) -> TokenStream2 {
let service_impl = parse_service_impl(service_impl_tokens);
check_service_single_by_name(&service_impl);
gen_gservice_impl(service_impl)
}

pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
let service_impl: ItemImpl = syn::parse2(service_impl_tokens).unwrap_or_else(|err| {
let service_impl = parse_service_impl(service_impl_tokens);
gen_gservice_impl(service_impl)
}

fn parse_service_impl(service_impl_tokens: TokenStream2) -> ItemImpl {
syn::parse2(service_impl_tokens).unwrap_or_else(|err| {
abort!(
err.span(),
"`gservice` attribute can be applied to impls only: {}",
err
)
});
})
}

let path = shared::impl_type_path(&service_impl);
fn check_service_single_by_name(service_impl: &ItemImpl) {
let path = shared::impl_type_path(service_impl);
let type_ident = path.path.segments.last().unwrap().ident.to_string();
if unsafe { shared::SERVICE_TYPES.get(&type_ident) }.is_some() {
abort!(
Expand All @@ -48,7 +61,9 @@ pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
)
}
unsafe { shared::SERVICE_TYPES.insert(type_ident, service_impl.span()) };
}

pub fn gen_gservice_impl(service_impl: ItemImpl) -> TokenStream2 {
let (service_type_path, service_type_args, service_type_constraints) = {
let service_type = ImplType::new(&service_impl);
(
Expand Down
4 changes: 2 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use proc_macro_error::proc_macro_error;
#[proc_macro_error]
#[proc_macro_attribute]
pub fn gservice(_attrs: TokenStream, impl_tokens: TokenStream) -> TokenStream {
sails_macros_core::gservice(impl_tokens.into()).into()
sails_macros_core::gservice_safe(impl_tokens.into()).into()
}

#[proc_macro_error]
#[proc_macro_attribute]
pub fn gprogram(_attrs: TokenStream, impl_tokens: TokenStream) -> TokenStream {
sails_macros_core::gprogram(impl_tokens.into()).into()
sails_macros_core::gprogram_safe(impl_tokens.into()).into()
}

#[proc_macro_error]
Expand Down

0 comments on commit e7f4fd6

Please sign in to comment.