@@ -871,6 +871,38 @@ impl ParameterInterface {
871871 }
872872}
873873
874+ pub trait StructuredParameters : Sized {
875+ fn declare_structured (
876+ node : & crate :: NodeState ,
877+ name : & str ,
878+ ) -> core:: result:: Result < Self , crate :: DeclarationError > ;
879+ }
880+
881+ impl < T : crate :: ParameterVariant > StructuredParameters for crate :: MandatoryParameter < T > {
882+ fn declare_structured (
883+ node : & crate :: NodeState ,
884+ name : & str ,
885+ ) -> core:: result:: Result < crate :: MandatoryParameter < T > , crate :: DeclarationError > {
886+ node. declare_parameter ( name) . mandatory ( )
887+ }
888+ }
889+ impl < T : crate :: ParameterVariant > StructuredParameters for crate :: OptionalParameter < T > {
890+ fn declare_structured (
891+ node : & crate :: NodeState ,
892+ name : & str ,
893+ ) -> core:: result:: Result < crate :: OptionalParameter < T > , crate :: DeclarationError > {
894+ node. declare_parameter ( name) . optional ( )
895+ }
896+ }
897+ impl < T : crate :: ParameterVariant > StructuredParameters for crate :: ReadOnlyParameter < T > {
898+ fn declare_structured (
899+ node : & crate :: NodeState ,
900+ name : & str ,
901+ ) -> core:: result:: Result < crate :: ReadOnlyParameter < T > , crate :: DeclarationError > {
902+ node. declare_parameter ( name) . read_only ( )
903+ }
904+ }
905+
874906#[ cfg( test) ]
875907mod tests {
876908 use super :: * ;
@@ -1410,4 +1442,66 @@ mod tests {
14101442 . optional ( )
14111443 . unwrap ( ) ;
14121444 }
1445+
1446+ use crate as rclrs;
1447+ use rclrs_proc_macros:: StructuredParameters ;
1448+
1449+ #[ derive( StructuredParameters , Debug ) ]
1450+ struct SimpleStructuredParameters {
1451+ _mandatory : MandatoryParameter < f64 > ,
1452+ _optional : OptionalParameter < f64 > ,
1453+ _readonly : ReadOnlyParameter < f64 > ,
1454+ }
1455+
1456+ #[ test]
1457+ fn test_simple_structured_parameters ( ) {
1458+ let args: Vec < String > = [
1459+ "test" ,
1460+ "--ros-args" ,
1461+ "-p" ,
1462+ "_mandatory:=1.0" ,
1463+ "-p" ,
1464+ "_optional:=1.0" ,
1465+ "-p" ,
1466+ "_readonly:=1.0" ,
1467+ ]
1468+ . into_iter ( )
1469+ . map ( str:: to_string)
1470+ . collect ( ) ;
1471+
1472+ let context = crate :: Context :: new ( args, InitOptions :: default ( ) ) . unwrap ( ) ;
1473+ let exec = context. create_basic_executor ( ) ;
1474+ let node = exec. create_node ( NodeOptions :: new ( "test" ) ) . unwrap ( ) ;
1475+ let _params = SimpleStructuredParameters :: declare_structured ( & node, "" ) . unwrap ( ) ;
1476+ }
1477+
1478+ #[ derive( StructuredParameters , Debug ) ]
1479+ struct NestedStructuredParameters {
1480+ _simple : SimpleStructuredParameters ,
1481+ _mandatory : MandatoryParameter < Arc < str > > ,
1482+ }
1483+
1484+ #[ test]
1485+ fn test_nested_structured_parameters ( ) {
1486+ let args: Vec < String > = [
1487+ "test" ,
1488+ "--ros-args" ,
1489+ "-p" ,
1490+ "nested._simple._mandatory:=1.0" ,
1491+ "-p" ,
1492+ "nested._simple._optional:=1.0" ,
1493+ "-p" ,
1494+ "nested._simple._readonly:=1.0" ,
1495+ "-p" ,
1496+ "nested._mandatory:=foo" ,
1497+ ]
1498+ . into_iter ( )
1499+ . map ( str:: to_string)
1500+ . collect ( ) ;
1501+
1502+ let context = crate :: Context :: new ( args, InitOptions :: default ( ) ) . unwrap ( ) ;
1503+ let exec = context. create_basic_executor ( ) ;
1504+ let node = exec. create_node ( NodeOptions :: new ( "test" ) ) . unwrap ( ) ;
1505+ let _params = NestedStructuredParameters :: declare_structured ( & node, "nested" ) . unwrap ( ) ;
1506+ }
14131507}
0 commit comments