Skip to content

Commit

Permalink
💩 Maybe fixed the macro export problem by using #[macro_use].
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 14, 2024
1 parent 8160e9f commit 8e1604b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 17 deletions.
53 changes: 41 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,33 @@ pub fn derive_struct(input: TokenStream) -> TokenStream {

let ret = if is_public {
quote! {
#[macro_use]
#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]
pub mod #mod_ident {
use super::*;

#( #structs )*
#( #enums )*
}

#( #structs_auto_macros )*
#( #enums_auto_macros )*
#( #structs_auto_macros )*
#( #enums_auto_macros )*
}

pub use #mod_ident::*;
}
} else {
quote! {
#[macro_use]
#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]
pub(crate) mod #mod_ident {
use super::*;

#( #structs )*
#( #enums )*
}

#( #structs_auto_macros )*
#( #enums_auto_macros )*
#( #structs_auto_macros )*
#( #enums_auto_macros )*
}

pub(crate) use #mod_ident::*;
}
Expand Down Expand Up @@ -103,31 +105,33 @@ pub fn derive_enum(input: TokenStream) -> TokenStream {

let ret = if is_public {
quote! {
#[macro_use]
#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]
pub mod #mod_ident {
use super::*;

#( #structs )*
#( #enums )*
}

#( #structs_auto_macros )*
#( #enums_auto_macros )*
#( #structs_auto_macros )*
#( #enums_auto_macros )*
}

pub use #mod_ident::*;
}
} else {
quote! {
#[macro_use]
#[allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]
pub(crate) mod #mod_ident {
use super::*;

#( #structs )*
#( #enums )*
}

#( #structs_auto_macros )*
#( #enums_auto_macros )*
#( #structs_auto_macros )*
#( #enums_auto_macros )*
}

pub(crate) use #mod_ident::*;
}
Expand Down Expand Up @@ -211,5 +215,30 @@ pub fn auto(input: TokenStream) -> TokenStream {
#ident::#key(#macro_ident!(#key 0 #next_key))
}
.into(),

AutoMacrosType::Value(items) => {
if items.len() == 1 {
let first_item = items.first().expect("Failed to get first item");
quote! {
#first_item
}
.into()
} else {
let list = items
.iter()
.enumerate()
.map(|(index, item)| {
quote! {
#macro_ident!(#index #item)
}
})
.collect::<Vec<_>>();

quote! {
(#( #list ),*)
}
.into()
}
}
}
}
24 changes: 23 additions & 1 deletion src/tools/auto_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum AutoMacrosType {
EnumStruct((Ident, Vec<(Ident, TokenStream)>)),
EnumTuple((Ident, Vec<TokenStream>)),
EnumSinglePath((Ident, TokenStream)),
Value(Vec<Expr>),
}

#[derive(Debug, Clone)]
Expand All @@ -26,6 +27,7 @@ impl Parse for AutoMacros {
let ident = input.parse()?;

if input.peek(token::Brace) {
// Str { key: ..., ... }
let content;
braced!(content in input);

Expand Down Expand Up @@ -84,6 +86,8 @@ impl Parse for AutoMacros {
body: AutoMacrosType::Struct(tokens),
})
} else if input.peek(Token![::]) {
// Sth::...

input.parse::<Token![::]>()?;
let key: Ident = input.parse()?;

Expand Down Expand Up @@ -188,7 +192,25 @@ impl Parse for AutoMacros {
})
}
} else {
Err(syn::Error::new(ident.span(), "Expected { or ::"))
// Sth(...)

let content;
parenthesized!(content in input);

let mut items = vec![];
while !content.is_empty() {
let value: Expr = content.parse()?;
items.push(value);

if content.peek(Token![,]) {
content.parse::<Token![,]>()?;
}
}

Ok(AutoMacros {
ident,
body: AutoMacrosType::Value(items),
})
}
}
}
13 changes: 9 additions & 4 deletions tests/auto_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ mod test {

#[test]
fn across_mod_auto() {
#[macro_use]
mod mod_a {
use yuuka::derive_struct;

Expand All @@ -156,12 +157,13 @@ mod test {
pub Root { a: String, b: i32 }
);

#[macro_use]
pub mod mod_b {
use yuuka::derive_enum;

derive_enum!(
#[derive(PartialEq)]
pub enum Root {
pub enum Root2 {
A,
B(i32),
}
Expand All @@ -171,16 +173,19 @@ mod test {

use yuuka::auto;

use mod_a::mod_b::*;
use mod_a::*;

assert_eq!(
auto!(mod_a::Root {
auto!(Root {
a: "hello".to_string(),
b: 42
}),
mod_a::Root {
Root {
a: "hello".to_string(),
b: 42
}
);
assert_eq!(auto!(mod_a::mod_b::Root::A), mod_a::mod_b::Root::A);
assert_eq!(auto!(Root2::A), Root2::A);
}
}

0 comments on commit 8e1604b

Please sign in to comment.