Skip to content

Commit

Permalink
✨ Try to allow import macros across crates.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 15, 2024
1 parent 8434208 commit ecc4848
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 228 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ jobs:
--all-targets \
--all-features \
--no-fail-fast
- name: Run across crate example test
run: /
cargo run \
--manifest-path \
./tests/across_crate_entry/Cargo.toml
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
target
Cargo.lock
197 changes: 0 additions & 197 deletions Cargo.lock

This file was deleted.

17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use template::{
generate_structs_quote,
};
use tools::{
auto_macros::AutoMacrosType, AutoMacros, DeriveEnum, DeriveStruct, DeriveVisibility, StructName,
auto_macros::AutoMacrosType, AutoMacros, DeriveBox, DeriveEnum, DeriveStruct, DeriveVisibility,
StructName,
};
use utils::flatten;

Expand All @@ -21,6 +22,7 @@ pub fn derive_struct(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveStruct);

let is_public = input.visibility == DeriveVisibility::Public;
let macro_visibility = input.extra_macros.macros_visibility;
let root_ident = match input.ident.clone() {
StructName::Named(v) => v,
StructName::Unnamed(_) => {
Expand All @@ -31,12 +33,12 @@ pub fn derive_struct(input: TokenStream) -> TokenStream {
let (structs, enums) = flatten(
root_ident.to_string(),
Rc::new(RefCell::new(0)),
tools::DeriveBox::Struct(input.clone()),
DeriveBox::Struct(input.clone()),
)
.expect("Failed to flatten");

let structs_auto_macros = generate_structs_auto_macros(structs.clone());
let enums_auto_macros = generate_enums_auto_macros(enums.clone());
let structs_auto_macros = generate_structs_auto_macros(structs.clone(), macro_visibility);
let enums_auto_macros = generate_enums_auto_macros(enums.clone(), macro_visibility);

let structs = generate_structs_quote(structs);
let enums = generate_enums_quote(enums);
Expand Down Expand Up @@ -83,6 +85,7 @@ pub fn derive_enum(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveEnum);

let is_public = input.visibility == DeriveVisibility::Public;
let macro_visibility = input.extra_macros.macros_visibility;
let root_ident = match input.ident.clone() {
StructName::Named(v) => v,
StructName::Unnamed(_) => {
Expand All @@ -93,12 +96,12 @@ pub fn derive_enum(input: TokenStream) -> TokenStream {
let (structs, enums) = flatten(
root_ident.to_string(),
Rc::new(RefCell::new(0)),
tools::DeriveBox::Enum(input.clone()),
DeriveBox::Enum(input.clone()),
)
.expect("Failed to flatten");

let structs_auto_macros = generate_structs_auto_macros(structs.clone());
let enums_auto_macros = generate_enums_auto_macros(enums.clone());
let structs_auto_macros = generate_structs_auto_macros(structs.clone(), macro_visibility);
let enums_auto_macros = generate_enums_auto_macros(enums.clone(), macro_visibility);

let structs = generate_structs_quote(structs);
let enums = generate_enums_quote(enums);
Expand Down
48 changes: 35 additions & 13 deletions src/template/auto_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;

use crate::tools::{EnumValueFlatten, EnumsFlatten};
use crate::tools::{DeriveAutoMacrosVisibility, EnumValueFlatten, EnumsFlatten};

pub(crate) fn generate_enums_auto_macros(enums: EnumsFlatten) -> Vec<TokenStream> {
pub(crate) fn generate_enums_auto_macros(
enums: EnumsFlatten,
macros_visibility: DeriveAutoMacrosVisibility,
) -> Vec<TokenStream> {
enums
.iter()
.map(|(ident, v, _default_value, _extra_macros)| {
Expand Down Expand Up @@ -57,19 +60,38 @@ pub(crate) fn generate_enums_auto_macros(enums: EnumsFlatten) -> Vec<TokenStream
};

let ident = Ident::new(format!("__auto_{}", ident).as_str(), ident.span());
quote! {
#[doc(hidden)]
macro_rules! #ident {
() => {};
if macros_visibility == DeriveAutoMacrosVisibility::Public {
quote! {
#[doc(hidden)]
#[macro_export]
macro_rules! #ident {
() => {};

#rules
#rules

($name:ident $key:ident $val:expr) => {
$val
};
($name:ident $val:expr) => {
$val
};
($name:ident $key:ident $val:expr) => {
$val
};
($name:ident $val:expr) => {
$val
};
}
}
} else {
quote! {
#[doc(hidden)]
macro_rules! #ident {
() => {};

#rules

($name:ident $key:ident $val:expr) => {
$val
};
($name:ident $val:expr) => {
$val
};
}
}
}
})
Expand Down
39 changes: 29 additions & 10 deletions src/template/auto_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::Ident;

use crate::tools::StructsFlatten;
use crate::tools::{DeriveAutoMacrosVisibility, StructsFlatten};

pub(crate) fn generate_structs_auto_macros(structs: StructsFlatten) -> Vec<TokenStream> {
pub(crate) fn generate_structs_auto_macros(
structs: StructsFlatten,
macros_visibility: DeriveAutoMacrosVisibility,
) -> Vec<TokenStream> {
structs
.iter()
.map(|(ident, v, _extra_macros)| {
Expand All @@ -23,16 +26,32 @@ pub(crate) fn generate_structs_auto_macros(structs: StructsFlatten) -> Vec<Token
};

let ident = Ident::new(format!("__auto_{}", ident).as_str(), ident.span());
quote! {
#[doc(hidden)]
macro_rules! #ident {
() => {};
if macros_visibility == DeriveAutoMacrosVisibility::Public {
quote! {
#[doc(hidden)]
#[macro_export]
macro_rules! #ident {
() => {};

#rules
#rules

($name:ident $val:expr) => {
$val
};
($name:ident $val:expr) => {
$val
};
}
}
} else {
quote! {
#[doc(hidden)]
macro_rules! #ident {
() => {};

#rules

($name:ident $val:expr) => {
$val
};
}
}
}
})
Expand Down
Loading

0 comments on commit ecc4848

Please sign in to comment.