diff --git a/gooey-macros/src/animation.rs b/gooey-macros/src/animation.rs index 2ffb9d1..2ef277a 100644 --- a/gooey-macros/src/animation.rs +++ b/gooey-macros/src/animation.rs @@ -1,16 +1,9 @@ -use attribute_derive::FromAttr; use manyhow::bail; use quote::ToTokens; use syn::{Field, ItemStruct}; use crate::*; -#[derive(FromAttr)] -#[attribute(ident = interpolate)] -struct FieldAttributes { - skip: bool, -} - pub fn linear_interpolate( ItemStruct { ident, @@ -28,38 +21,46 @@ pub fn linear_interpolate( fields => fields .into_iter() .enumerate() - .map(|(idx, Field { attrs, ident, .. })| { + .map(|(idx, Field { ident, .. })| { let ident = ident .map(ToTokens::into_token_stream) .unwrap_or_else(|| proc_macro2::Literal::usize_unsuffixed(idx).into_token_stream()); - Ok(if FieldAttributes::from_attributes(&attrs)?.skip { - quote!(#ident: __target.#ident,) - } else { quote!(#ident: ::gooey::animation::LinearInterpolate::lerp(&self.#ident, &__target.#ident, __percent),) - }) }), - }.collect::()?; + }; Ok(quote! { impl ::gooey::animation::LinearInterpolate for #ident { fn lerp(&self, __target: &Self, __percent: f32) -> Self { - #ident{#fields} + #ident{#(#fields)*} } } }) } +#[cfg(test)] +macro_rules! expansion_snapshot { + (#[derive($fn:expr)]$($tokens:tt)*) => {{ + use insta::assert_snapshot; + use prettyplease::unparse; + use syn::{parse2, parse_quote}; + let input = parse_quote!($($tokens)*); + let output = $fn(input).unwrap(); + assert_snapshot!(unparse(&parse2(output).unwrap())) + }}; +} + #[test] fn test() { - use insta::assert_snapshot; - use prettyplease::unparse; - use syn::{parse2, parse_quote}; - let input = parse_quote!( + expansion_snapshot! { + #[derive(linear_interpolate)] struct HelloWorld { fielda: Hello, fieldb: World, } - ); - let output = linear_interpolate(input).unwrap(); - assert_snapshot!(unparse(&parse2(output).unwrap())) + }; + expansion_snapshot! { + #[derive(linear_interpolate)] + struct HelloWorld(Hello, World); + }; } diff --git a/gooey-macros/src/snapshots/gooey_macros__animation__test-2.snap b/gooey-macros/src/snapshots/gooey_macros__animation__test-2.snap new file mode 100644 index 0000000..117910f --- /dev/null +++ b/gooey-macros/src/snapshots/gooey_macros__animation__test-2.snap @@ -0,0 +1,21 @@ +--- +source: gooey-macros/src/animation.rs +expression: unparse(&parse2(output).unwrap()) +--- +impl ::gooey::animation::LinearInterpolate for HelloWorld { + fn lerp(&self, __target: &Self, __percent: f32) -> Self { + HelloWorld { + 0: ::gooey::animation::LinearInterpolate::lerp( + &self.0, + &__target.0, + __percent, + ), + 1: ::gooey::animation::LinearInterpolate::lerp( + &self.1, + &__target.1, + __percent, + ), + } + } +} + diff --git a/src/styles/components.rs b/src/styles/components.rs index 02d15a2..2e95ef4 100644 --- a/src/styles/components.rs +++ b/src/styles/components.rs @@ -30,7 +30,7 @@ use crate::styles::{Dimension, FocusableWidgets, VisualOrder}; /// /// This component defaults to picking a contrasting color between `TextColor` and `SurfaceColor` /// ContrastingColor(Color, "contrasting_color", contrasting!(ThemedComponent, TextColor, SurfaceColor)) /// /// This component shows how to use a closure for nearly infinite flexibility in computing the default value. -/// ClosureDefaultComponent(Color, "closure_component", |context| context.query_style(&TextColor)) +/// ClosureDefaultComponent(Color, "closure_component", |context| context.get(&TextColor)) /// } /// } /// ```