Add the unit, display_decimal_places, and step parameter widget macro attributes (#2706)

* UI working for spacing enum.

* Implementations.

* UI working for spacing enum.

* Undo all changes.

* unit, display_decimal_places, and step macro implementation.

* Fixing tests.

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Albar 2025-06-14 20:29:06 -04:00 committed by Keavon Chambers
parent f3720bf6f2
commit 643d7b4432
4 changed files with 132 additions and 10 deletions

View file

@ -54,6 +54,9 @@ pub struct FieldMetadata {
pub number_min: Option<f64>,
pub number_max: Option<f64>,
pub number_mode_range: Option<(f64, f64)>,
pub number_display_decimal_places: Option<u32>,
pub number_step: Option<f64>,
pub unit: Option<&'static str>,
}
pub trait ChoiceTypeStatic: Sized + Copy + crate::vector::misc::AsU32 + Send + Sync {

View file

@ -163,6 +163,41 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
_ => quote!(None),
})
.collect();
let number_display_decimal_places: Vec<_> = fields
.iter()
.map(|field| match field {
ParsedField::Regular {
number_display_decimal_places: Some(decimal_places),
..
}
| ParsedField::Node {
number_display_decimal_places: Some(decimal_places),
..
} => {
quote!(Some(#decimal_places))
}
_ => quote!(None),
})
.collect();
let number_step: Vec<_> = fields
.iter()
.map(|field| match field {
ParsedField::Regular { number_step: Some(step), .. } | ParsedField::Node { number_step: Some(step), .. } => {
quote!(Some(#step))
}
_ => quote!(None),
})
.collect();
let unit_suffix: Vec<_> = fields
.iter()
.map(|field| match field {
ParsedField::Regular { unit: Some(unit), .. } | ParsedField::Node { unit: Some(unit), .. } => {
quote!(Some(#unit))
}
_ => quote!(None),
})
.collect();
let exposed: Vec<_> = fields
.iter()
@ -375,6 +410,9 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
number_min: #number_min_values,
number_max: #number_max_values,
number_mode_range: #number_mode_range_values,
number_display_decimal_places: #number_display_decimal_places,
number_step: #number_step,
unit: #unit_suffix,
},
)*
],

View file

@ -7,7 +7,8 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::{Comma, RArrow};
use syn::{
AttrStyle, Attribute, Error, Expr, ExprTuple, FnArg, GenericParam, Ident, ItemFn, Lit, LitFloat, LitStr, Meta, Pat, PatIdent, PatType, Path, ReturnType, Type, TypeParam, WhereClause, parse_quote,
AttrStyle, Attribute, Error, Expr, ExprTuple, FnArg, GenericParam, Ident, ItemFn, Lit, LitFloat, LitInt, LitStr, Meta, Pat, PatIdent, PatType, Path, ReturnType, Type, TypeParam, WhereClause,
parse_quote,
};
use crate::codegen::generate_node_code;
@ -110,7 +111,10 @@ pub(crate) enum ParsedField {
number_hard_min: Option<LitFloat>,
number_hard_max: Option<LitFloat>,
number_mode_range: Option<ExprTuple>,
number_display_decimal_places: Option<LitInt>,
number_step: Option<LitFloat>,
implementations: Punctuated<Type, Comma>,
unit: Option<LitStr>,
},
Node {
pat_ident: PatIdent,
@ -119,7 +123,10 @@ pub(crate) enum ParsedField {
widget_override: ParsedWidgetOverride,
input_type: Type,
output_type: Type,
number_display_decimal_places: Option<LitInt>,
number_step: Option<LitFloat>,
implementations: Punctuated<Implementation, Comma>,
unit: Option<LitStr>,
},
}
#[derive(Debug)]
@ -466,6 +473,35 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul
}
}
let unit = extract_attribute(attrs, "unit")
.map(|attr| attr.parse_args::<LitStr>().map_err(|e| Error::new_spanned(attr, format!("Expected a unit type as string"))))
.transpose()?;
let number_display_decimal_places = extract_attribute(attrs, "display_decimal_places")
.map(|attr| {
attr.parse_args::<LitInt>().map_err(|e| {
Error::new_spanned(
attr,
format!("Invalid `integer` for number of decimals for argument '{}': {}\nUSAGE EXAMPLE: #[display_decimal_places(2)]", ident, e),
)
})
})
.transpose()?
.map(|f| {
if let Err(e) = f.base10_parse::<u32>() {
Err(Error::new_spanned(f, format!("Expected a `u32` for `display_decimal_places` for '{}': {}", ident, e)))
} else {
Ok(f)
}
})
.transpose()?;
let number_step = extract_attribute(attrs, "step")
.map(|attr| {
attr.parse_args::<LitFloat>()
.map_err(|e| Error::new_spanned(attr, format!("Invalid `step` for argument '{}': {}\nUSAGE EXAMPLE: #[step(2.)]", ident, e)))
})
.transpose()?;
let (is_node, node_input_type, node_output_type) = parse_node_type(&ty);
let description = attrs
.iter()
@ -502,7 +538,10 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul
widget_override,
input_type,
output_type,
number_display_decimal_places,
number_step,
implementations,
unit,
})
} else {
let implementations = extract_attribute(attrs, "implementations")
@ -520,9 +559,12 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul
number_hard_min,
number_hard_max,
number_mode_range,
number_display_decimal_places,
number_step,
ty,
value_source,
implementations,
unit,
})
}
}
@ -738,7 +780,10 @@ mod tests {
number_hard_min: None,
number_hard_max: None,
number_mode_range: None,
number_display_decimal_places: None,
number_step: None,
implementations: Punctuated::new(),
unit: None,
}],
body: TokenStream2::new(),
crate_name: FoundCrate::Itself,
@ -790,7 +835,10 @@ mod tests {
widget_override: ParsedWidgetOverride::None,
input_type: parse_quote!(Footprint),
output_type: parse_quote!(T),
number_display_decimal_places: None,
number_step: None,
implementations: Punctuated::new(),
unit: None,
},
ParsedField::Regular {
pat_ident: pat_ident("translate"),
@ -805,7 +853,10 @@ mod tests {
number_hard_min: None,
number_hard_max: None,
number_mode_range: None,
number_display_decimal_places: None,
number_step: None,
implementations: Punctuated::new(),
unit: None,
},
],
body: TokenStream2::new(),
@ -860,7 +911,10 @@ mod tests {
number_hard_min: None,
number_hard_max: None,
number_mode_range: None,
number_display_decimal_places: None,
number_step: None,
implementations: Punctuated::new(),
unit: None,
}],
body: TokenStream2::new(),
crate_name: FoundCrate::Itself,
@ -913,12 +967,15 @@ mod tests {
number_hard_min: None,
number_hard_max: None,
number_mode_range: None,
number_display_decimal_places: None,
number_step: None,
implementations: {
let mut p = Punctuated::new();
p.push(parse_quote!(f32));
p.push(parse_quote!(f64));
p
},
unit: None,
}],
body: TokenStream2::new(),
crate_name: FoundCrate::Itself,
@ -978,7 +1035,10 @@ mod tests {
number_hard_min: None,
number_hard_max: None,
number_mode_range: Some(parse_quote!((0., 100.))),
number_display_decimal_places: None,
number_step: None,
implementations: Punctuated::new(),
unit: None,
}],
body: TokenStream2::new(),
crate_name: FoundCrate::Itself,
@ -1031,7 +1091,10 @@ mod tests {
number_hard_min: None,
number_hard_max: None,
number_mode_range: None,
number_display_decimal_places: None,
number_step: None,
implementations: Punctuated::new(),
unit: None,
}],
body: TokenStream2::new(),
crate_name: FoundCrate::Itself,