Skip to content

Commit 6ace7f6

Browse files
committed
Add validation code
1 parent ce36a23 commit 6ace7f6

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

node-graph/node-macro/src/codegen.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
135135
.iter()
136136
.map(|field| match field {
137137
ParsedField::Regular { number_soft_min, number_hard_min, .. } => match (number_soft_min, number_hard_min) {
138-
(Some(soft_min), None) => quote!(Some(#soft_min)),
138+
(Some(soft_min), _) => quote!(Some(#soft_min)),
139139
(None, Some(hard_min)) => quote!(Some(#hard_min)),
140140
(None, None) => quote!(None),
141-
(Some(soft_min), Some(hard_min)) => quote!(#graphene_core::num_traits::clamp_min(#soft_min, #hard_min)),
142141
},
143142
_ => quote!(None),
144143
})
@@ -147,10 +146,9 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result<TokenStre
147146
.iter()
148147
.map(|field| match field {
149148
ParsedField::Regular { number_soft_max, number_hard_max, .. } => match (number_soft_max, number_hard_max) {
150-
(Some(soft_max), None) => quote!(Some(#soft_max)),
149+
(Some(soft_max), _) => quote!(Some(#soft_max)),
151150
(None, Some(hard_max)) => quote!(Some(#hard_max)),
152151
(None, None) => quote!(None),
153-
(Some(soft_max), Some(hard_max)) => quote!(#graphene_core::num_traits::clamp_max(#soft_max, #hard_max)),
154152
},
155153
_ => quote!(None),
156154
})

node-graph/node-macro/src/validation.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn validate_node_fn(parsed: &ParsedNodeFn) -> syn::Result<()> {
99
// Add more validators here as needed
1010
validate_implementations_for_generics,
1111
validate_primary_input_expose,
12+
validate_min_max,
1213
];
1314

1415
for validator in validators {
@@ -18,6 +19,73 @@ pub fn validate_node_fn(parsed: &ParsedNodeFn) -> syn::Result<()> {
1819
Ok(())
1920
}
2021

22+
fn validate_min_max(parsed: &ParsedNodeFn) {
23+
for field in &parsed.fields {
24+
match field {
25+
ParsedField::Regular {
26+
number_hard_max,
27+
number_hard_min,
28+
number_soft_max,
29+
number_soft_min,
30+
pat_ident,
31+
..
32+
} => {
33+
match (number_soft_min, number_hard_min) {
34+
(Some(soft_min), Some(hard_min)) => {
35+
let soft_min_value: f64 = soft_min.base10_parse().unwrap_or_default();
36+
let hard_min_value: f64 = hard_min.base10_parse().unwrap_or_default();
37+
if soft_min_value == hard_min_value {
38+
emit_error!(
39+
pat_ident.span(),
40+
"Unnecessary #[soft_min] attribute on `{}` as #[hard_min] equals to it.",
41+
pat_ident.ident;
42+
help = "You can safely remove the #[soft_min] attribute from this field.";
43+
note = "The #[hard_min] also limits the range to same without #[soft_min]",
44+
);
45+
} else if soft_min_value < hard_min_value {
46+
emit_error!(
47+
pat_ident.span(),
48+
"The #[soft_min] attribute exists on `{}` and is lower than #[hard_min].",
49+
pat_ident.ident;
50+
help = "You probably meant to reverse the two macros";
51+
note = "Allowing the possible range in slider to be more than #[hard_min] doesn't make sense",
52+
);
53+
}
54+
}
55+
_ => (),
56+
}
57+
58+
match (number_soft_max, number_hard_max) {
59+
(Some(soft_max), Some(hard_max)) => {
60+
let soft_max_value: f64 = soft_max.base10_parse().unwrap_or_default();
61+
let hard_max_value: f64 = hard_max.base10_parse().unwrap_or_default();
62+
if soft_max_value == hard_max_value {
63+
emit_error!(
64+
pat_ident.span(),
65+
"Unnecessary #[soft_max] attribute on `{}` as #[hard_max] equals to it.",
66+
pat_ident.ident;
67+
help = "You can safely remove the #[soft_max] attribute from this field.";
68+
note = "The #[hard_max] also limits the range to same without #[soft_max]",
69+
);
70+
} else if soft_max_value < hard_max_value {
71+
emit_error!(
72+
pat_ident.span(),
73+
"The #[soft_max] attribute exists on `{}` and is greater than #[hard_max].",
74+
pat_ident.ident;
75+
help = "You probably meant to reverse the two macros";
76+
note = "Allowing the possible range in slider to be more than #[hard_max] doesn't make sense",
77+
);
78+
}
79+
}
80+
_ => (),
81+
}
82+
}
83+
84+
_ => (),
85+
}
86+
}
87+
}
88+
2189
fn validate_primary_input_expose(parsed: &ParsedNodeFn) {
2290
if let Some(ParsedField::Regular { exposed: true, pat_ident, .. }) = parsed.fields.first() {
2391
emit_error!(

0 commit comments

Comments
 (0)