mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-19 10:58:04 +08:00
node-macro: cleanup ParsedField struct (#3064)
* node-macro: cleanup `ParsedField` struct * node-macro: fixup tests
This commit is contained in:
@@ -14,7 +14,7 @@ use syn::{
|
||||
use crate::codegen::generate_node_code;
|
||||
use crate::shader_nodes::ShaderNodeType;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Implementation {
|
||||
pub(crate) input: Type,
|
||||
pub(crate) _arrow: RArrow,
|
||||
@@ -53,7 +53,7 @@ pub(crate) struct NodeFnAttributes {
|
||||
// Add more attributes as needed
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub enum ParsedValueSource {
|
||||
#[default]
|
||||
None,
|
||||
@@ -64,7 +64,7 @@ pub enum ParsedValueSource {
|
||||
// #[widget(ParsedWidgetOverride::Hidden)]
|
||||
// #[widget(ParsedWidgetOverride::String = "Some string")]
|
||||
// #[widget(ParsedWidgetOverride::Custom = "Custom string")]
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub enum ParsedWidgetOverride {
|
||||
#[default]
|
||||
None,
|
||||
@@ -102,39 +102,44 @@ impl Parse for ParsedWidgetOverride {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum ParsedField {
|
||||
Regular {
|
||||
pat_ident: PatIdent,
|
||||
name: Option<LitStr>,
|
||||
description: String,
|
||||
widget_override: ParsedWidgetOverride,
|
||||
ty: Type,
|
||||
exposed: bool,
|
||||
value_source: ParsedValueSource,
|
||||
number_soft_min: Option<LitFloat>,
|
||||
number_soft_max: Option<LitFloat>,
|
||||
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,
|
||||
name: Option<LitStr>,
|
||||
description: String,
|
||||
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(Clone, Debug)]
|
||||
pub struct ParsedField {
|
||||
pub pat_ident: PatIdent,
|
||||
pub name: Option<LitStr>,
|
||||
pub description: String,
|
||||
pub widget_override: ParsedWidgetOverride,
|
||||
pub ty: ParsedFieldType,
|
||||
pub number_display_decimal_places: Option<LitInt>,
|
||||
pub number_step: Option<LitFloat>,
|
||||
pub unit: Option<LitStr>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ParsedFieldType {
|
||||
Regular(RegularParsedField),
|
||||
Node(NodeParsedField),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct RegularParsedField {
|
||||
pub ty: Type,
|
||||
pub exposed: bool,
|
||||
pub value_source: ParsedValueSource,
|
||||
pub number_soft_min: Option<LitFloat>,
|
||||
pub number_soft_max: Option<LitFloat>,
|
||||
pub number_hard_min: Option<LitFloat>,
|
||||
pub number_hard_max: Option<LitFloat>,
|
||||
pub number_mode_range: Option<ExprTuple>,
|
||||
pub implementations: Punctuated<Type, Comma>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct NodeParsedField {
|
||||
pub input_type: Type,
|
||||
pub output_type: Type,
|
||||
pub implementations: Punctuated<Implementation, Comma>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Input {
|
||||
pub(crate) pat_ident: PatIdent,
|
||||
@@ -563,16 +568,18 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
|
||||
Ok(ParsedField::Node {
|
||||
Ok(ParsedField {
|
||||
pat_ident,
|
||||
ty: ParsedFieldType::Node(NodeParsedField {
|
||||
input_type,
|
||||
output_type,
|
||||
implementations,
|
||||
}),
|
||||
name,
|
||||
description,
|
||||
widget_override,
|
||||
input_type,
|
||||
output_type,
|
||||
number_display_decimal_places,
|
||||
number_step,
|
||||
implementations,
|
||||
unit,
|
||||
})
|
||||
} else {
|
||||
@@ -580,22 +587,24 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul
|
||||
.map(|attr| parse_implementations(attr, ident))
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
Ok(ParsedField::Regular {
|
||||
Ok(ParsedField {
|
||||
pat_ident,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
exposed,
|
||||
number_soft_min,
|
||||
number_soft_max,
|
||||
number_hard_min,
|
||||
number_hard_max,
|
||||
number_mode_range,
|
||||
ty,
|
||||
value_source,
|
||||
implementations,
|
||||
}),
|
||||
name,
|
||||
description,
|
||||
widget_override,
|
||||
exposed,
|
||||
number_soft_min,
|
||||
number_soft_max,
|
||||
number_hard_min,
|
||||
number_hard_max,
|
||||
number_mode_range,
|
||||
number_display_decimal_places,
|
||||
number_step,
|
||||
ty,
|
||||
value_source,
|
||||
implementations,
|
||||
unit,
|
||||
})
|
||||
}
|
||||
@@ -715,18 +724,24 @@ mod tests {
|
||||
for (parsed_field, expected_field) in parsed.fields.iter().zip(expected.fields.iter()) {
|
||||
match (parsed_field, expected_field) {
|
||||
(
|
||||
ParsedField::Regular {
|
||||
ParsedField {
|
||||
pat_ident: p_name,
|
||||
ty: p_ty,
|
||||
exposed: p_exp,
|
||||
value_source: p_default,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: p_ty,
|
||||
exposed: p_exp,
|
||||
value_source: p_default,
|
||||
..
|
||||
}),
|
||||
..
|
||||
},
|
||||
ParsedField::Regular {
|
||||
ParsedField {
|
||||
pat_ident: e_name,
|
||||
ty: e_ty,
|
||||
exposed: e_exp,
|
||||
value_source: e_default,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: e_ty,
|
||||
exposed: e_exp,
|
||||
value_source: e_default,
|
||||
..
|
||||
}),
|
||||
..
|
||||
},
|
||||
) => {
|
||||
@@ -745,16 +760,22 @@ mod tests {
|
||||
assert_eq!(format!("{:?}", p_ty), format!("{:?}", e_ty));
|
||||
}
|
||||
(
|
||||
ParsedField::Node {
|
||||
ParsedField {
|
||||
pat_ident: p_name,
|
||||
input_type: p_input,
|
||||
output_type: p_output,
|
||||
ty: ParsedFieldType::Node(NodeParsedField {
|
||||
input_type: p_input,
|
||||
output_type: p_output,
|
||||
..
|
||||
}),
|
||||
..
|
||||
},
|
||||
ParsedField::Node {
|
||||
ParsedField {
|
||||
pat_ident: e_name,
|
||||
input_type: e_input,
|
||||
output_type: e_output,
|
||||
ty: ParsedFieldType::Node(NodeParsedField {
|
||||
input_type: e_input,
|
||||
output_type: e_output,
|
||||
..
|
||||
}),
|
||||
..
|
||||
},
|
||||
) => {
|
||||
@@ -802,22 +823,24 @@ mod tests {
|
||||
},
|
||||
output_type: parse_quote!(f64),
|
||||
is_async: false,
|
||||
fields: vec![ParsedField::Regular {
|
||||
fields: vec![ParsedField {
|
||||
pat_ident: pat_ident("b"),
|
||||
name: None,
|
||||
description: String::new(),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
implementations: Punctuated::new(),
|
||||
}),
|
||||
number_display_decimal_places: None,
|
||||
number_step: None,
|
||||
implementations: Punctuated::new(),
|
||||
unit: None,
|
||||
}],
|
||||
body: TokenStream2::new(),
|
||||
@@ -866,34 +889,38 @@ mod tests {
|
||||
output_type: parse_quote!(T),
|
||||
is_async: false,
|
||||
fields: vec![
|
||||
ParsedField::Node {
|
||||
ParsedField {
|
||||
pat_ident: pat_ident("transform_target"),
|
||||
name: None,
|
||||
description: String::new(),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
input_type: parse_quote!(Footprint),
|
||||
output_type: parse_quote!(T),
|
||||
ty: ParsedFieldType::Node(NodeParsedField {
|
||||
input_type: parse_quote!(Footprint),
|
||||
output_type: parse_quote!(T),
|
||||
implementations: Punctuated::new(),
|
||||
}),
|
||||
number_display_decimal_places: None,
|
||||
number_step: None,
|
||||
implementations: Punctuated::new(),
|
||||
unit: None,
|
||||
},
|
||||
ParsedField::Regular {
|
||||
ParsedField {
|
||||
pat_ident: pat_ident("translate"),
|
||||
name: None,
|
||||
description: String::new(),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
ty: parse_quote!(DVec2),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: parse_quote!(DVec2),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
implementations: Punctuated::new(),
|
||||
}),
|
||||
number_display_decimal_places: None,
|
||||
number_step: None,
|
||||
implementations: Punctuated::new(),
|
||||
unit: None,
|
||||
},
|
||||
],
|
||||
@@ -939,22 +966,24 @@ mod tests {
|
||||
},
|
||||
output_type: parse_quote!(Vector),
|
||||
is_async: false,
|
||||
fields: vec![ParsedField::Regular {
|
||||
fields: vec![ParsedField {
|
||||
pat_ident: pat_ident("radius"),
|
||||
name: None,
|
||||
description: String::new(),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::Default(quote!(50.)),
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::Default(quote!(50.)),
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
implementations: Punctuated::new(),
|
||||
}),
|
||||
number_display_decimal_places: None,
|
||||
number_step: None,
|
||||
implementations: Punctuated::new(),
|
||||
unit: None,
|
||||
}],
|
||||
body: TokenStream2::new(),
|
||||
@@ -998,27 +1027,29 @@ mod tests {
|
||||
},
|
||||
output_type: parse_quote!(Table<Raster<P>>),
|
||||
is_async: false,
|
||||
fields: vec![ParsedField::Regular {
|
||||
fields: vec![ParsedField {
|
||||
pat_ident: pat_ident("shadows"),
|
||||
name: None,
|
||||
description: String::new(),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
implementations: {
|
||||
let mut p = Punctuated::new();
|
||||
p.push(parse_quote!(f32));
|
||||
p.push(parse_quote!(f64));
|
||||
p
|
||||
},
|
||||
}),
|
||||
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(),
|
||||
@@ -1069,22 +1100,24 @@ mod tests {
|
||||
},
|
||||
output_type: parse_quote!(f64),
|
||||
is_async: false,
|
||||
fields: vec![ParsedField::Regular {
|
||||
fields: vec![ParsedField {
|
||||
pat_ident: pat_ident("b"),
|
||||
name: None,
|
||||
description: String::from("b"),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: Some(parse_quote!(-500.)),
|
||||
number_soft_max: Some(parse_quote!(500.)),
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: Some(parse_quote!((0., 100.))),
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: parse_quote!(f64),
|
||||
exposed: false,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: Some(parse_quote!(-500.)),
|
||||
number_soft_max: Some(parse_quote!(500.)),
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: Some(parse_quote!((0., 100.))),
|
||||
implementations: Punctuated::new(),
|
||||
}),
|
||||
number_display_decimal_places: None,
|
||||
number_step: None,
|
||||
implementations: Punctuated::new(),
|
||||
unit: None,
|
||||
}],
|
||||
body: TokenStream2::new(),
|
||||
@@ -1128,22 +1161,24 @@ mod tests {
|
||||
},
|
||||
output_type: parse_quote!(Table<Raster<CPU>>),
|
||||
is_async: true,
|
||||
fields: vec![ParsedField::Regular {
|
||||
fields: vec![ParsedField {
|
||||
pat_ident: pat_ident("path"),
|
||||
name: None,
|
||||
ty: parse_quote!(String),
|
||||
description: String::new(),
|
||||
widget_override: ParsedWidgetOverride::None,
|
||||
exposed: true,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
ty: ParsedFieldType::Regular(RegularParsedField {
|
||||
ty: parse_quote!(String),
|
||||
exposed: true,
|
||||
value_source: ParsedValueSource::None,
|
||||
number_soft_min: None,
|
||||
number_soft_max: None,
|
||||
number_hard_min: None,
|
||||
number_hard_max: None,
|
||||
number_mode_range: None,
|
||||
implementations: Punctuated::new(),
|
||||
}),
|
||||
number_display_decimal_places: None,
|
||||
number_step: None,
|
||||
implementations: Punctuated::new(),
|
||||
unit: None,
|
||||
}],
|
||||
body: TokenStream2::new(),
|
||||
|
||||
Reference in New Issue
Block a user