Skip to content

Commit d71fda4

Browse files
authored
Add support for constants to message generation (#269)
This will produce: ``` impl VariousTypes { /// binary, hexadecimal and octal constants are also possible pub const TWO_PLUS_TWO: i8 = 5; /// Only unbounded strings are possible pub const PASSWORD: &'static str = "hunter2"; /// As determined by Edward J. Goodwin pub const PI: f32 = 3.0; } ```
1 parent d03eafa commit d71fda4

File tree

6 files changed

+72
-27
lines changed

6 files changed

+72
-27
lines changed

rclrs_example_msgs/msg/VariousTypes.msg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ NestedType[2] nested_array
3232
NestedType[] nested_seq_unbounded
3333
NestedType[<=3] nested_seq_bounded
3434

35+
36+
# binary, hexadecimal and octal constants are also possible
37+
int8 TWO_PLUS_TWO = 5
38+
# Only unbounded strings are possible
39+
string PASSWORD = "hunter2"
40+
# As determined by Edward J. Goodwin
41+
float32 PI = 3.0

rosidl_generator_rs/resource/msg.rs.em

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ TEMPLATE(
55
package_name=package_name, interface_path=interface_path,
66
msg_specs=msg_specs,
77
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
8-
get_idiomatic_rs_type=get_idiomatic_rs_type)
8+
get_idiomatic_rs_type=get_idiomatic_rs_type,
9+
constant_value_to_rs=constant_value_to_rs)
910
}@
1011
} // mod rmw
1112

@@ -15,5 +16,6 @@ TEMPLATE(
1516
package_name=package_name, interface_path=interface_path,
1617
msg_specs=msg_specs,
1718
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
18-
get_idiomatic_rs_type=get_idiomatic_rs_type)
19+
get_idiomatic_rs_type=get_idiomatic_rs_type,
20+
constant_value_to_rs=constant_value_to_rs)
1921
}@

rosidl_generator_rs/resource/msg_idiomatic.rs.em

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@{
2+
from rosidl_parser.definition import AbstractGenericString
23
from rosidl_parser.definition import Array
34
from rosidl_parser.definition import BasicType
45
from rosidl_parser.definition import BoundedSequence
@@ -29,6 +30,30 @@ pub struct @(type_name) {
2930
@[end for]@
3031
}
3132

33+
@[if msg_spec.constants]@
34+
impl @(type_name) {
35+
@[for constant in msg_spec.constants]@
36+
@{
37+
comments = getattr(constant, 'get_comment_lines', lambda: [])()
38+
}@
39+
@[ for line in comments]@
40+
@[ if line]@
41+
/// @(line)
42+
@[ else]@
43+
///
44+
@[ end if]@
45+
@[ end for]@
46+
@[ if isinstance(constant.type, BasicType)]@
47+
pub const @(get_rs_name(constant.name)): @(get_rmw_rs_type(constant.type)) = @(constant_value_to_rs(constant.type, constant.value));
48+
@[ elif isinstance(constant.type, AbstractGenericString)]@
49+
pub const @(get_rs_name(constant.name)): &'static str = @(constant_value_to_rs(constant.type, constant.value));
50+
@[ else]@
51+
@{assert False, 'Unhandled constant type: ' + str(constant.type)}@
52+
@[ end if]@
53+
@[end for]@
54+
}
55+
@[end if]
56+
3257
impl Default for @(type_name) {
3358
fn default() -> Self {
3459
@# This has the benefit of automatically setting the right default values

rosidl_generator_rs/resource/msg_rmw.rs.em

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@{
2+
from rosidl_parser.definition import AbstractGenericString
23
from rosidl_parser.definition import Array
34
from rosidl_parser.definition import BasicType
45
from rosidl_parser.definition import BoundedSequence
@@ -11,7 +12,7 @@ from rosidl_parser.definition import UnboundedWString
1112
}@
1213
#[cfg(feature = "serde")]
1314
use serde::{Deserialize, Serialize};
14-
@[for subfolder, msg_spec in msg_specs]@
15+
@[for subfolder, msg_spec in msg_specs]@
1516
@{
1617
type_name = msg_spec.structure.namespaced_type.name
1718
}@
@@ -40,6 +41,30 @@ pub struct @(type_name) {
4041
@[end for]@
4142
}
4243

44+
@[if msg_spec.constants]@
45+
impl @(type_name) {
46+
@[for constant in msg_spec.constants]@
47+
@{
48+
comments = getattr(constant, 'get_comment_lines', lambda: [])()
49+
}@
50+
@[ for line in comments]@
51+
@[ if line]@
52+
/// @(line)
53+
@[ else]@
54+
///
55+
@[ end if]@
56+
@[ end for]@
57+
@[ if isinstance(constant.type, BasicType)]@
58+
pub const @(get_rs_name(constant.name)): @(get_rmw_rs_type(constant.type)) = @(constant_value_to_rs(constant.type, constant.value));
59+
@[ elif isinstance(constant.type, AbstractGenericString)]@
60+
pub const @(get_rs_name(constant.name)): &'static str = @(constant_value_to_rs(constant.type, constant.value));
61+
@[ else]@
62+
@{assert False, 'Unhandled constant type: ' + str(constant.type)}@
63+
@[ end if]@
64+
@[end for]@
65+
}
66+
@[end if]
67+
4368
impl Default for @(type_name) {
4469
fn default() -> Self {
4570
unsafe {
@@ -54,8 +79,6 @@ impl Default for @(type_name) {
5479
}
5580
}
5681
57-
58-
5982
impl rosidl_runtime_rs::SequenceAlloc for @(type_name) {
6083
fn sequence_init(seq: &mut rosidl_runtime_rs::Sequence<Self>, size: libc::size_t) -> bool {
6184
// SAFETY: This is safe since a the point is guaranteed to be valid/initialized.

rosidl_generator_rs/resource/srv.rs.em

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ TEMPLATE(
1212
package_name=package_name, interface_path=interface_path,
1313
msg_specs=req_res_specs,
1414
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
15-
get_idiomatic_rs_type=get_idiomatic_rs_type)
15+
get_idiomatic_rs_type=get_idiomatic_rs_type,
16+
constant_value_to_rs=constant_value_to_rs)
1617
}@
1718

1819
@[for subfolder, srv_spec in srv_specs]
@@ -48,7 +49,8 @@ TEMPLATE(
4849
package_name=package_name, interface_path=interface_path,
4950
msg_specs=req_res_specs,
5051
get_rs_name=get_rs_name, get_rmw_rs_type=get_rmw_rs_type,
51-
get_idiomatic_rs_type=get_idiomatic_rs_type)
52+
get_idiomatic_rs_type=get_idiomatic_rs_type,
53+
constant_value_to_rs=constant_value_to_rs)
5254
}@
5355

5456
@[for subfolder, srv_spec in srv_specs]

rosidl_generator_rs/rosidl_generator_rs/__init__.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,28 +243,14 @@ def primitive_value_to_rs(type_, value):
243243
def constant_value_to_rs(type_, value):
244244
assert value is not None
245245

246-
if type_ == 'bool':
247-
return 'true' if value else 'false'
248-
249-
if type_ in [
250-
'byte',
251-
'char',
252-
'int8',
253-
'uint8',
254-
'int16',
255-
'uint16',
256-
'int32',
257-
'uint32',
258-
'int64',
259-
'uint64',
260-
'float64',
261-
]:
246+
if isinstance(type_, BasicType):
247+
if type_.typename == 'boolean':
248+
return 'true' if value else 'false'
249+
elif type_.typename == 'float32':
250+
return '%sf' % value
262251
return str(value)
263252

264-
if type_ == 'float32':
265-
return '%sf' % value
266-
267-
if type_ == 'string':
253+
if isinstance(type_, AbstractGenericString):
268254
return '"%s"' % escape_string(value)
269255

270256
assert False, "unknown constant type '%s'" % type_

0 commit comments

Comments
 (0)