Skip to content

Commit d9bcf97

Browse files
committed
Add some documentation
1 parent 2561549 commit d9bcf97

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

rclrs/src/parameter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use crate::vendor::rcl_interfaces::msg::rmw::{ParameterType, ParameterValue as R
1414
use crate::{
1515
call_string_getter_with_rcl_node, rcl_bindings::*, Node, RclrsError, ENTITY_LIFECYCLE_MUTEX,
1616
};
17-
use std::default;
1817
use std::{
1918
collections::{btree_map::Entry, BTreeMap},
2019
fmt::Debug,

rclrs/src/parameter/structured.rs

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,74 @@
11
use crate::NodeState;
22

3+
/// Marker trait for implementing [`StructuredParameters`] where a default value cannot be specified.
4+
/// This is usually the case for container types, that are not represented by any actual parameter.
35
pub enum DefaultForbidden {}
46

5-
pub trait StructuredParametersMeta<T>: Sized {
6-
fn declare_structured_(
7+
/// Types implementing this trait can delcare their parameters with[`NodeState::declare_parameters`].
8+
/// The trait can be automatically derived using [`rclrs_proc_macros`] if:
9+
/// - if the type is a struct
10+
/// - all attributes implement [`StructuredParameters`]
11+
pub trait StructuredParameters: Sized {
12+
/// Declares all parameters in ros node.
13+
///
14+
/// # Parameters
15+
///
16+
/// - `node`: The ros node to declare parameters for.
17+
/// - `name`: The name of the parameter. Nested parameters are recursively declared with "{name}.{field_name}" if the name is not empty else "{field_name}".
18+
/// - `default`: The default value for the paramter.
19+
/// - `description` The description of the parameter
20+
///
21+
/// # Returns
22+
///
23+
/// [`Result`] containing the declared structured parameters or [`crate::DeclarationError`]
24+
fn declare_structured<T>(
725
node: &NodeState,
826
name: &str,
927
default: Option<T>,
1028
description: impl Into<std::sync::Arc<str>>,
11-
) -> core::result::Result<Self, crate::DeclarationError>;
29+
) -> core::result::Result<Self, crate::DeclarationError>
30+
where
31+
Self: StructuredParametersMeta<T>,
32+
{
33+
Self::declare_structured_(node, name, default, description)
34+
}
1235
}
1336

14-
pub trait StructuredParameters: Sized {
15-
fn declare_structured<T>(
37+
/// Helper trait to unify the default value type with generic container types like
38+
/// - [`crate::MandatoryParameter<T>`]
39+
/// - [`crate::OptionalParameter<T>`]
40+
/// - [`crate::ReadOnlyParameter<T>`]
41+
///
42+
/// In these cases the [`Self`] != [`T`] and forces the usage of a generic trait.
43+
/// However, a generic trait also requires annotating this default value in the derive macro.
44+
/// For the container based structured parameters [`T`] is always [`DefaultForbidden`]
45+
/// and therefore we can hide this form the trait + macro by using this helper trait.
46+
/// The previously mentioned leaf types that actually hold parameters, are to be implemented manually anyway.
47+
///
48+
pub trait StructuredParametersMeta<T>: Sized {
49+
/// See [`StructuredParameters::declare_structured`]
50+
fn declare_structured_(
1651
node: &NodeState,
1752
name: &str,
1853
default: Option<T>,
1954
description: impl Into<std::sync::Arc<str>>,
20-
) -> core::result::Result<Self, crate::DeclarationError>
55+
) -> core::result::Result<Self, crate::DeclarationError>;
56+
}
57+
58+
impl NodeState {
59+
/// Declares all nested parameters of the [`StructuredParameters`].
60+
/// Parameter naming recursively follows "`name`.`field_name`" or "`field_name`" if the initial `name` is empty.
61+
pub fn declare_parameters<T, T0>(
62+
&self,
63+
name: &str,
64+
) -> core::result::Result<T, crate::DeclarationError>
2165
where
22-
Self: StructuredParametersMeta<T>,
66+
T: StructuredParameters + StructuredParametersMeta<T0>,
2367
{
24-
Self::declare_structured_(node, name, default, description)
68+
T::declare_structured(self, name, None, "")
2569
}
2670
}
71+
2772
impl<T: crate::ParameterVariant> StructuredParameters for crate::MandatoryParameter<T> {}
2873
impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::MandatoryParameter<T> {
2974
fn declare_structured_(
@@ -73,18 +118,7 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::Optional
73118
}
74119
}
75120

76-
impl NodeState {
77-
pub fn declare_parameters<T, T0>(
78-
&self,
79-
name: &str,
80-
) -> core::result::Result<T, crate::DeclarationError>
81-
where
82-
T: StructuredParameters + StructuredParametersMeta<T0>,
83-
{
84-
T::declare_structured(self, name, None, "")
85-
}
86-
}
87-
121+
#[cfg(test)]
88122
mod tests {
89123
use std::sync::Arc;
90124

rclrs_proc_macros/src/impl.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use std::default;
2-
31
use proc_macro2::TokenStream;
42
use quote::quote;
5-
use syn::{Data, DeriveInput, Expr, Lit, Meta};
3+
use syn::{DeriveInput, Expr};
64

75
pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenStream> {
86
let ident = input.ident;
@@ -37,7 +35,7 @@ pub(crate) fn derive_struct_parameters(input: DeriveInput) -> syn::Result<TokenS
3735
description = Some(meta.value()?.parse()?);
3836
Ok(())
3937
} else {
40-
let err = format!("Unknown key: {:?}", &meta.path);
38+
let err = format!("Unknown key: {:?}", &meta.path.get_ident());
4139
syn::Result::Err(syn::Error::new_spanned(meta.path, err))
4240
}
4341
})?;

0 commit comments

Comments
 (0)