mirror of
https://github.com/danbulant/cushy
synced 2026-05-24 12:28:23 +00:00
remove skip from derive(LinearInterpolate)
This commit is contained in:
parent
4fa5be0cec
commit
5c9500460c
3 changed files with 44 additions and 22 deletions
|
|
@ -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::<Result>()?;
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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))
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
|
|
|
|||
Loading…
Reference in a new issue