Skip to content

Commit 2561549

Browse files
committed
Add support for description
1 parent 2ba7748 commit 2561549

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

rclrs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub use error::*;
213213
pub use executor::*;
214214
pub use logging::*;
215215
pub use node::*;
216-
pub use parameter::structured::{ParameterOptions, StructuredParameters, StructuredParametersMeta};
216+
pub use parameter::structured::{StructuredParameters, StructuredParametersMeta};
217217
pub use parameter::*;
218218
pub use publisher::*;
219219
pub use qos::*;

rclrs/src/parameter/structured.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub trait StructuredParametersMeta<T>: Sized {
77
node: &NodeState,
88
name: &str,
99
default: Option<T>,
10+
description: impl Into<std::sync::Arc<str>>,
1011
) -> core::result::Result<Self, crate::DeclarationError>;
1112
}
1213

@@ -15,11 +16,12 @@ pub trait StructuredParameters: Sized {
1516
node: &NodeState,
1617
name: &str,
1718
default: Option<T>,
19+
description: impl Into<std::sync::Arc<str>>,
1820
) -> core::result::Result<Self, crate::DeclarationError>
1921
where
2022
Self: StructuredParametersMeta<T>,
2123
{
22-
Self::declare_structured_(node, name, default)
24+
Self::declare_structured_(node, name, default, description)
2325
}
2426
}
2527
impl<T: crate::ParameterVariant> StructuredParameters for crate::MandatoryParameter<T> {}
@@ -28,13 +30,14 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::Mandator
2830
node: &NodeState,
2931
name: &str,
3032
default: Option<T>,
33+
description: impl Into<std::sync::Arc<str>>,
3134
) -> core::result::Result<Self, crate::DeclarationError> {
3235
let builder = node.declare_parameter(name);
3336
let builder = match default {
3437
Some(default) => builder.default(default),
3538
None => builder,
3639
};
37-
builder.mandatory()
40+
builder.description(description).mandatory()
3841
}
3942
}
4043
impl<T: crate::ParameterVariant> StructuredParameters for crate::ReadOnlyParameter<T> {}
@@ -43,13 +46,14 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::ReadOnly
4346
node: &NodeState,
4447
name: &str,
4548
default: Option<T>,
49+
description: impl Into<std::sync::Arc<str>>,
4650
) -> core::result::Result<Self, crate::DeclarationError> {
4751
let builder = node.declare_parameter(name);
4852
let builder = match default {
4953
Some(default) => builder.default(default),
5054
None => builder,
5155
};
52-
builder.read_only()
56+
builder.description(description).read_only()
5357
}
5458
}
5559
impl<T: crate::ParameterVariant> StructuredParameters for crate::OptionalParameter<T> {}
@@ -58,13 +62,14 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::Optional
5862
node: &NodeState,
5963
name: &str,
6064
default: Option<T>,
65+
description: impl Into<std::sync::Arc<str>>,
6166
) -> core::result::Result<Self, crate::DeclarationError> {
6267
let builder = node.declare_parameter(name);
6368
let builder = match default {
6469
Some(default) => builder.default(default),
6570
None => builder,
6671
};
67-
builder.optional()
72+
builder.description(description).optional()
6873
}
6974
}
7075

@@ -76,7 +81,7 @@ impl NodeState {
7681
where
7782
T: StructuredParameters + StructuredParametersMeta<T0>,
7883
{
79-
T::declare_structured(self, name, None)
84+
T::declare_structured(self, name, None, "")
8085
}
8186
}
8287

@@ -171,4 +176,27 @@ mod tests {
171176
let _params: SimpleStructuredParametersWithDefaults = node.declare_parameters("").unwrap();
172177
println!("{:?}", _params);
173178
}
179+
#[derive(Debug, StructuredParameters)]
180+
struct SimpleStructuredParametersWithDefaultsAndDescriptions {
181+
#[param(default = 42.0, description = "_mandatory")]
182+
_mandatory: rclrs::MandatoryParameter<f64>,
183+
#[param(default = 42.0, description = "_optional")]
184+
_optional: rclrs::OptionalParameter<f64>,
185+
#[param(default = Arc::from("test"), description = "_readonly")]
186+
_readonly: rclrs::ReadOnlyParameter<Arc<str>>,
187+
}
188+
189+
#[test]
190+
fn test_simple_structured_parameters_with_defaults_and_descriptions() {
191+
let args: Vec<String> = ["test", "--ros-args"]
192+
.into_iter()
193+
.map(str::to_string)
194+
.collect();
195+
let context = crate::Context::new(args, rclrs::InitOptions::default()).unwrap();
196+
let exec = context.create_basic_executor();
197+
let node = exec.create_node(rclrs::NodeOptions::new("test")).unwrap();
198+
let _params: SimpleStructuredParametersWithDefaultsAndDescriptions =
199+
node.declare_parameters("").unwrap();
200+
println!("{:?}", _params);
201+
}
174202
}

rclrs_proc_macros/src/impl.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenS
2525
let ident_str = syn::LitStr::new(&f.ident.as_ref().unwrap().to_string(), ident.span());
2626

2727
let mut default: Option<Expr> = None;
28+
let mut description: Option<Expr> = None;
2829

2930
for attr in &f.attrs {
3031
if attr.path().is_ident("param") {
3132
attr.parse_nested_meta(|meta| {
3233
if meta.path.is_ident("default") {
3334
default = Some(meta.value()?.parse()?);
3435
Ok(())
36+
} else if meta.path.is_ident("description") {
37+
description = Some(meta.value()?.parse()?);
38+
Ok(())
3539
} else {
36-
syn::Result::Err(syn::Error::new_spanned(meta.path, "Unknown key."))
40+
let err = format!("Unknown key: {:?}", &meta.path);
41+
syn::Result::Err(syn::Error::new_spanned(meta.path, err))
3742
}
3843
})?;
3944
}
@@ -43,6 +48,10 @@ pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenS
4348
Some(expr) => quote! {Some(#expr)},
4449
None => quote! {None},
4550
};
51+
let description = match description {
52+
Some(expr) => quote! {#expr},
53+
None => quote! {""},
54+
};
4655

4756
let field_type = match &f.ty {
4857
syn::Type::Path(p) => {
@@ -67,6 +76,8 @@ pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenS
6776
prefix => [prefix, ".", #ident_str].concat(),
6877
}},
6978
#default,
79+
#description,
80+
7081
)?,
7182
};
7283
args.push(r);
@@ -86,6 +97,7 @@ pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenS
8697
node: &rclrs::NodeState,
8798
name: &str,
8899
default: Option<DefaultForbidden>,
100+
description: impl Into<std::sync::Arc<str>>,
89101
)
90102
-> core::result::Result<Self, crate::DeclarationError> {
91103
core::result::Result::Ok(Self{ #(#args)*})

0 commit comments

Comments
 (0)