Skip to content

Commit

Permalink
fix naming and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vobradovich committed May 17, 2024
1 parent 4ff7402 commit edab091
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 45 deletions.
5 changes: 1 addition & 4 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[target.x86_64-pc-windows-msvc]
rustflags = [
"-Clink-arg=/force:unresolved",
"-Ctarget-feature=+crt-static",
]
rustflags = ["-Clink-arg=/force:unresolved", "-Ctarget-feature=+crt-static"]
4 changes: 2 additions & 2 deletions macros/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

//! Implemntation of the procedural macros exposed via the `sails-macros` crate.
pub use program::__gprogram_internal;
pub use program::gprogram;
pub use program::gprogram_safe;
pub use route::groute;
pub use service::__gservice_internal;
pub use service::gservice;
pub use service::gservice_safe;

mod program;
mod route;
Expand Down
22 changes: 13 additions & 9 deletions macros/core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ 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);
/// Static Span of Program `impl` block
static mut PROGRAM_SPAN: Option<Span> = None;

pub fn gprogram(program_impl_tokens: TokenStream2) -> TokenStream2 {
let program_impl = parse_gprogram_impl(program_impl_tokens);
ensure_single_gprogram(&program_impl);
gen_gprogram_impl(program_impl)
}

pub fn gprogram(program_impl_tokens: TokenStream2) -> TokenStream2 {
let program_impl = parse_program_impl(program_impl_tokens);
#[doc(hidden)]
pub fn __gprogram_internal(program_impl_tokens: TokenStream2) -> TokenStream2 {
let program_impl = parse_gprogram_impl(program_impl_tokens);
gen_gprogram_impl(program_impl)
}

fn parse_program_impl(program_impl_tokens: TokenStream2) -> ItemImpl {
fn parse_gprogram_impl(program_impl_tokens: TokenStream2) -> ItemImpl {
syn::parse2(program_impl_tokens).unwrap_or_else(|err| {
abort!(
err.span(),
Expand All @@ -33,14 +37,14 @@ fn parse_program_impl(program_impl_tokens: TokenStream2) -> ItemImpl {
})
}

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

fn gen_gprogram_impl(program_impl: ItemImpl) -> TokenStream2 {
Expand Down
25 changes: 14 additions & 11 deletions macros/core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ 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);
static mut SERVICE_SPANS: BTreeMap<String, Span> = BTreeMap::new();

pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
let service_impl = parse_gservice_impl(service_impl_tokens);
ensure_single_gservice_by_name(&service_impl);
gen_gservice_impl(service_impl)
}

pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
let service_impl = parse_service_impl(service_impl_tokens);
#[doc(hidden)]
pub fn __gservice_internal(service_impl_tokens: TokenStream2) -> TokenStream2 {
let service_impl = parse_gservice_impl(service_impl_tokens);
gen_gservice_impl(service_impl)
}

fn parse_service_impl(service_impl_tokens: TokenStream2) -> ItemImpl {
fn parse_gservice_impl(service_impl_tokens: TokenStream2) -> ItemImpl {
syn::parse2(service_impl_tokens).unwrap_or_else(|err| {
abort!(
err.span(),
Expand All @@ -54,19 +57,19 @@ fn parse_service_impl(service_impl_tokens: TokenStream2) -> ItemImpl {
})
}

fn check_service_single_by_name(service_impl: &ItemImpl) {
fn ensure_single_gservice_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() {
if unsafe { SERVICE_SPANS.get(&type_ident) }.is_some() {
abort!(
service_impl.span(),
"multiple `gservice` attributes are not allowed"
"multiple `gservice` attributes on a type with the same name are not allowed"
)
}
unsafe { shared::SERVICE_TYPES.insert(type_ident, service_impl.span()) };
unsafe { SERVICE_SPANS.insert(type_ident, service_impl.span()) };
}

pub fn gen_gservice_impl(service_impl: ItemImpl) -> TokenStream2 {
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
7 changes: 1 addition & 6 deletions macros/core/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::route;
use proc_macro2::{Span, TokenStream as TokenStream2};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::abort;
use quote::{quote, ToTokens};
use std::collections::BTreeMap;
Expand All @@ -8,11 +8,6 @@ use syn::{
ReturnType, Signature, Type, TypePath, TypeTuple, WhereClause,
};

pub(crate) static mut SERVICE_TYPES: BTreeMap<String, Span> = BTreeMap::new();

/// Static Span of Program `impl` block
pub(crate) static mut PROGRAM_SPAN: Option<Span> = None;

/// A struct that represents the type of an `impl` block.
pub(crate) struct ImplType<'a> {
path: &'a TypePath,
Expand Down
12 changes: 6 additions & 6 deletions macros/core/tests/program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use quote::quote;
use sails_macros_core::gprogram;
use sails_macros_core::__gprogram_internal;

#[test]
fn gprogram_generates_init_for_single_ctor() {
Expand All @@ -11,7 +11,7 @@ fn gprogram_generates_init_for_single_ctor() {
}
};

let result = gprogram(input).to_string();
let result = __gprogram_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
Expand All @@ -31,7 +31,7 @@ fn gprogram_generates_init_for_multiple_ctors() {
}
};

let result = gprogram(input).to_string();
let result = __gprogram_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
Expand All @@ -44,7 +44,7 @@ fn gprogram_generates_init_for_no_ctor() {
}
};

let result = gprogram(input).to_string();
let result = __gprogram_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
Expand All @@ -60,7 +60,7 @@ fn gprogram_generates_handle_for_single_service_with_non_empty_route() {
}
};

let result = gprogram(input).to_string();
let result = __gprogram_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
Expand All @@ -81,7 +81,7 @@ fn gprogram_generates_handle_for_multiple_services_with_non_empty_routes() {
}
};

let result = gprogram(input).to_string();
let result = __gprogram_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
Expand Down
6 changes: 3 additions & 3 deletions macros/core/tests/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use quote::quote;
use sails_macros_core::gservice;
use sails_macros_core::__gservice_internal;

#[test]
fn gservice_works() {
Expand All @@ -15,7 +15,7 @@ fn gservice_works() {
}
};

let result = gservice(input).to_string();
let result = __gservice_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
Expand All @@ -33,7 +33,7 @@ fn gservice_works_for_lifetimes_and_generics() {
}
};

let result = gservice(input).to_string();
let result = __gservice_internal(input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
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_safe(impl_tokens.into()).into()
sails_macros_core::gservice(impl_tokens.into()).into()
}

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

#[proc_macro_error]
Expand Down
2 changes: 1 addition & 1 deletion macros/tests/ui/gservice_fails_multiple_not_allowed.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: multiple `gservice` attributes are not allowed
error: multiple `gservice` attributes on a type with the same name are not allowed
--> tests/ui/gservice_fails_multiple_not_allowed.rs:13:1
|
13 | impl MyService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: multiple `gservice` attributes are not allowed
error: multiple `gservice` attributes on a type with the same name are not allowed
--> tests/ui/gservice_fails_multiple_not_allowed_on_one_impl.rs:7:1
|
7 | impl MyService {
Expand Down

0 comments on commit edab091

Please sign in to comment.