Skip to content

Commit

Permalink
preserve other macros (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgunozerk authored Nov 28, 2024
1 parent 58ce8f1 commit f355931
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/type_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ pub fn type_state_inner(args: TokenStream, input: TokenStream) -> TokenStream {
.map(|ident| quote!(::core::marker::PhantomData<fn() -> #ident>))
.collect::<Vec<_>>();

// Get the struct's attributes (other macros) excluding the #[type_state] macro
let attrs: Vec<_> = input_struct
.attrs
.iter()
.filter(|attr| !attr.path().is_ident("type_state"))
.collect();

// Generate the final output
let output = quote! {
mod #sealed_mod_name {
Expand All @@ -138,6 +145,7 @@ pub fn type_state_inner(args: TokenStream, input: TokenStream) -> TokenStream {

#(#trait_impls)*

#(#attrs)*
#[allow(clippy::type_complexity)]
#visibility struct #struct_name<#combined_generics>
#merged_where_clause
Expand Down
8 changes: 8 additions & 0 deletions tests/simple_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum Race {
Human,
}

#[derive(Debug)]
#[type_state(states = (Initial, RaceSet, LevelSet, SkillSlotsSet), slots = (Initial))]
struct PlayerBuilder {
race: Option<Race>,
Expand Down Expand Up @@ -133,4 +134,11 @@ mod tests {
assert_eq!(player.level, another_player.level);
assert_eq!(player.skill_slots, another_player.skill_slots);
}

#[test]
fn other_macros_are_preserved() {
let player = PlayerBuilder::new();
println!("{:?}", player); // ensures `#[derive(Debug)]` is preserved
assert_eq!(player.level, None);
}
}

0 comments on commit f355931

Please sign in to comment.