remove skip from derive(LinearInterpolate)

This commit is contained in:
Roland Fredenhagen 2023-11-13 17:46:57 +01:00
parent 4fa5be0cec
commit 5c9500460c
No known key found for this signature in database
GPG key ID: 094AF99241035EB6
3 changed files with 44 additions and 22 deletions

View file

@ -1,16 +1,9 @@
use attribute_derive::FromAttr;
use manyhow::bail; use manyhow::bail;
use quote::ToTokens; use quote::ToTokens;
use syn::{Field, ItemStruct}; use syn::{Field, ItemStruct};
use crate::*; use crate::*;
#[derive(FromAttr)]
#[attribute(ident = interpolate)]
struct FieldAttributes {
skip: bool,
}
pub fn linear_interpolate( pub fn linear_interpolate(
ItemStruct { ItemStruct {
ident, ident,
@ -28,38 +21,46 @@ pub fn linear_interpolate(
fields => fields fields => fields
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(|(idx, Field { attrs, ident, .. })| { .map(|(idx, Field { ident, .. })| {
let ident = ident let ident = ident
.map(ToTokens::into_token_stream) .map(ToTokens::into_token_stream)
.unwrap_or_else(|| proc_macro2::Literal::usize_unsuffixed(idx).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),) quote!(#ident: ::gooey::animation::LinearInterpolate::lerp(&self.#ident, &__target.#ident, __percent),)
})
}), }),
}.collect::<Result>()?; };
Ok(quote! { Ok(quote! {
impl ::gooey::animation::LinearInterpolate for #ident { impl ::gooey::animation::LinearInterpolate for #ident {
fn lerp(&self, __target: &Self, __percent: f32) -> Self { 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] #[test]
fn test() { fn test() {
use insta::assert_snapshot; expansion_snapshot! {
use prettyplease::unparse; #[derive(linear_interpolate)]
use syn::{parse2, parse_quote};
let input = parse_quote!(
struct HelloWorld { struct HelloWorld {
fielda: Hello, fielda: Hello,
fieldb: World, fieldb: World,
} }
); };
let output = linear_interpolate(input).unwrap(); expansion_snapshot! {
assert_snapshot!(unparse(&parse2(output).unwrap())) #[derive(linear_interpolate)]
struct HelloWorld(Hello, World);
};
} }

View file

@ -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,
),
}
}
}

View file

@ -30,7 +30,7 @@ use crate::styles::{Dimension, FocusableWidgets, VisualOrder};
/// /// This component defaults to picking a contrasting color between `TextColor` and `SurfaceColor` /// /// This component defaults to picking a contrasting color between `TextColor` and `SurfaceColor`
/// ContrastingColor(Color, "contrasting_color", contrasting!(ThemedComponent, TextColor, 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. /// /// 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))
/// } /// }
/// } /// }
/// ``` /// ```