Skip to content

Commit 74cf511

Browse files
Extend macro with more builder options
1 parent 0879517 commit 74cf511

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

rclrs-macros/src/impl.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro2::TokenStream;
22
use quote::quote;
3-
use syn::{DeriveInput, Expr};
3+
use syn::{token::Token, DeriveInput, Expr};
44

55
pub(crate) fn derive_structured_parameters(input: DeriveInput) -> syn::Result<TokenStream> {
66
let ident = input.ident;
@@ -15,6 +15,7 @@ pub(crate) fn derive_structured_parameters(input: DeriveInput) -> syn::Result<To
1515
}
1616
}
1717
}
18+
1819
fn derive_structured_parameters_struct(
1920
ident: proc_macro2::Ident,
2021
struct_: &syn::DataStruct,
@@ -29,6 +30,9 @@ fn derive_structured_parameters_struct(
2930

3031
let mut default: Option<Expr> = None;
3132
let mut description: Option<Expr> = None;
33+
let mut constraints: Option<Expr> = None;
34+
let mut ignore_override = false;
35+
let mut discard_mismatching_prior_value = false;
3236

3337
for attr in &f.attrs {
3438
if attr.path().is_ident("param") {
@@ -39,6 +43,15 @@ fn derive_structured_parameters_struct(
3943
} else if meta.path.is_ident("description") {
4044
description = Some(meta.value()?.parse()?);
4145
Ok(())
46+
} else if meta.path.is_ident("constraints") {
47+
constraints = Some(meta.value()?.parse()?);
48+
Ok(())
49+
} else if meta.path.is_ident("ignore_override") {
50+
ignore_override = true;
51+
Ok(())
52+
} else if meta.path.is_ident("discard_mismatching_prior_value") {
53+
discard_mismatching_prior_value = true;
54+
Ok(())
4255
} else {
4356
let err = format!("Unknown key: {:?}", &meta.path.get_ident());
4457
syn::Result::Err(syn::Error::new_spanned(meta.path, err))
@@ -55,6 +68,10 @@ fn derive_structured_parameters_struct(
5568
Some(expr) => quote! {#expr},
5669
None => quote! {""},
5770
};
71+
let constraints = match constraints {
72+
Some(expr) => quote! {#expr},
73+
None => quote! {""},
74+
};
5875

5976
let field_type = match &f.ty {
6077
syn::Type::Path(p) => {
@@ -80,6 +97,9 @@ fn derive_structured_parameters_struct(
8097
}},
8198
#default,
8299
#description,
100+
#constraints,
101+
#ignore_override,
102+
#discard_mismatching_prior_value,
83103

84104
)?,
85105
};
@@ -101,6 +121,9 @@ fn derive_structured_parameters_struct(
101121
name: &str,
102122
default: Option<DefaultForbidden>,
103123
description: impl Into<std::sync::Arc<str>>,
124+
constraints: impl Into<std::sync::Arc<str>>,
125+
ignore_override: bool,
126+
discard_mismatching_prior_value: bool,
104127
)
105128
-> core::result::Result<Self, crate::DeclarationError> {
106129
core::result::Result::Ok(Self{ #(#args)*})

rclrs/src/parameter/structured.rs

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,22 @@ pub trait StructuredParameters: Sized {
3030
name: &str,
3131
default: Option<T>,
3232
description: impl Into<std::sync::Arc<str>>,
33+
constraints: impl Into<std::sync::Arc<str>>,
34+
ignore_override: bool,
35+
discard_mismatching_prior_value: bool,
3336
) -> core::result::Result<Self, crate::DeclarationError>
3437
where
3538
Self: StructuredParametersMeta<T>,
3639
{
37-
Self::declare_structured_(node, name, default, description)
40+
Self::declare_structured_(
41+
node,
42+
name,
43+
default,
44+
description,
45+
constraints,
46+
ignore_override,
47+
discard_mismatching_prior_value,
48+
)
3849
}
3950
}
4051

@@ -56,6 +67,9 @@ pub trait StructuredParametersMeta<T>: Sized {
5667
name: &str,
5768
default: Option<T>,
5869
description: impl Into<std::sync::Arc<str>>,
70+
constraints: impl Into<std::sync::Arc<str>>,
71+
ignore_override: bool,
72+
discard_mismatching_prior_value: bool,
5973
) -> core::result::Result<Self, crate::DeclarationError>;
6074
}
6175

@@ -69,7 +83,7 @@ impl NodeState {
6983
where
7084
T: StructuredParameters + StructuredParametersMeta<T0>,
7185
{
72-
T::declare_structured(self, name, None, "")
86+
T::declare_structured(self, name, None, "", "", false, false)
7387
}
7488
}
7589

@@ -80,13 +94,27 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::Mandator
8094
name: &str,
8195
default: Option<T>,
8296
description: impl Into<std::sync::Arc<str>>,
97+
constraints: impl Into<std::sync::Arc<str>>,
98+
ignore_override: bool,
99+
discard_mismatching_prior_value: bool,
83100
) -> core::result::Result<Self, crate::DeclarationError> {
84101
let builder = node.declare_parameter(name);
85102
let builder = match default {
86103
Some(default) => builder.default(default),
87104
None => builder,
88105
};
89-
builder.description(description).mandatory()
106+
let builder = match ignore_override {
107+
true => builder.ignore_override(),
108+
false => builder,
109+
};
110+
let builder = match discard_mismatching_prior_value {
111+
true => builder.discard_mismatching_prior_value(),
112+
false => builder,
113+
};
114+
builder
115+
.description(description)
116+
.constraints(constraints)
117+
.mandatory()
90118
}
91119
}
92120
impl<T: crate::ParameterVariant> StructuredParameters for crate::ReadOnlyParameter<T> {}
@@ -96,13 +124,27 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::ReadOnly
96124
name: &str,
97125
default: Option<T>,
98126
description: impl Into<std::sync::Arc<str>>,
127+
constraints: impl Into<std::sync::Arc<str>>,
128+
ignore_override: bool,
129+
discard_mismatching_prior_value: bool,
99130
) -> core::result::Result<Self, crate::DeclarationError> {
100131
let builder = node.declare_parameter(name);
101132
let builder = match default {
102133
Some(default) => builder.default(default),
103134
None => builder,
104135
};
105-
builder.description(description).read_only()
136+
let builder = match ignore_override {
137+
true => builder.ignore_override(),
138+
false => builder,
139+
};
140+
let builder = match discard_mismatching_prior_value {
141+
true => builder.discard_mismatching_prior_value(),
142+
false => builder,
143+
};
144+
builder
145+
.description(description)
146+
.constraints(constraints)
147+
.read_only()
106148
}
107149
}
108150
impl<T: crate::ParameterVariant> StructuredParameters for crate::OptionalParameter<T> {}
@@ -112,13 +154,31 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::Optional
112154
name: &str,
113155
default: Option<T>,
114156
description: impl Into<std::sync::Arc<str>>,
157+
constraints: impl Into<std::sync::Arc<str>>,
158+
ignore_override: bool,
159+
discard_mismatching_prior_value: bool,
115160
) -> core::result::Result<Self, crate::DeclarationError> {
116161
let builder = node.declare_parameter(name);
117162
let builder = match default {
118163
Some(default) => builder.default(default),
119164
None => builder,
120165
};
121-
builder.description(description).optional()
166+
let builder = match ignore_override {
167+
true => builder.ignore_override(),
168+
false => builder,
169+
};
170+
let builder = match discard_mismatching_prior_value {
171+
true => builder.discard_mismatching_prior_value(),
172+
false => builder,
173+
};
174+
175+
//builder.discriminate(f)
176+
//builder.range(range)
177+
178+
builder
179+
.description(description)
180+
.constraints(constraints)
181+
.optional()
122182
}
123183
}
124184

@@ -216,7 +276,7 @@ mod tests {
216276
}
217277
#[derive(Debug, StructuredParameters)]
218278
struct SimpleStructuredParametersWithDefaultsAndDescriptions {
219-
#[param(default = 42.0, description = "_mandatory")]
279+
#[param(default = 42.0, ignore_override, description = "_mandatory")]
220280
_mandatory: rclrs::MandatoryParameter<f64>,
221281
#[param(default = 42.0, description = "_optional")]
222282
_optional: rclrs::OptionalParameter<f64>,

0 commit comments

Comments
 (0)