|
1 | 1 | use crate::NodeState;
|
2 | 2 |
|
| 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. |
3 | 5 | pub enum DefaultForbidden {}
|
4 | 6 |
|
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>( |
7 | 25 | node: &NodeState,
|
8 | 26 | name: &str,
|
9 | 27 | default: Option<T>,
|
10 | 28 | 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 | + } |
12 | 35 | }
|
13 | 36 |
|
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_( |
16 | 51 | node: &NodeState,
|
17 | 52 | name: &str,
|
18 | 53 | default: Option<T>,
|
19 | 54 | 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> |
21 | 65 | where
|
22 |
| - Self: StructuredParametersMeta<T>, |
| 66 | + T: StructuredParameters + StructuredParametersMeta<T0>, |
23 | 67 | {
|
24 |
| - Self::declare_structured_(node, name, default, description) |
| 68 | + T::declare_structured(self, name, None, "") |
25 | 69 | }
|
26 | 70 | }
|
| 71 | + |
27 | 72 | impl<T: crate::ParameterVariant> StructuredParameters for crate::MandatoryParameter<T> {}
|
28 | 73 | impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::MandatoryParameter<T> {
|
29 | 74 | fn declare_structured_(
|
@@ -73,18 +118,7 @@ impl<T: crate::ParameterVariant> StructuredParametersMeta<T> for crate::Optional
|
73 | 118 | }
|
74 | 119 | }
|
75 | 120 |
|
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)] |
88 | 122 | mod tests {
|
89 | 123 | use std::sync::Arc;
|
90 | 124 |
|
|
0 commit comments