__init__.py
Go to the documentation of this file.
1 import genmsg.msgs
2 
3 try:
4  from cStringIO import StringIO #Python 2.x
5 except ImportError:
6  from io import StringIO #Python 3.x
7 
8 MSG_TYPE_TO_RS = {
9  'byte': 'i8',
10  'char': 'u8',
11  'bool': 'bool',
12  'uint8': 'u8',
13  'uint16': 'u16',
14  'uint32': 'u32',
15  'uint64': 'u64',
16  'int8': 'i8',
17  'int16': 'i16',
18  'int32': 'i32',
19  'int64': 'i64',
20  'float32': 'f32',
21  'float64': 'f64',
22  'string': '::std::string::String',
23  'time': 'u32',
24  'duration': 'i32',
25 }
26 
27 
28 def depending_on(spec):
29  """
30  Creates a list of unique packages based on dependencies
31  """
32  packages = []
33  for field in spec.parsed_fields():
34  if (not field.is_builtin):
35  if (field.is_header):
36  packages.append('std_msgs')
37  else:
38  (package,
39  name) = genmsg.names.package_resource_name(field.base_type)
40  packages.append((package or spec.package) + ' ' +
41  name) # convert '' to package
42  return set(packages)
43 
44 
45 def msg_type_to_rs(type, package):
46  """
47  Converts a message type (e.g. uint32, std_msgs/String, etc.) into the Rust declaration
48  for that type (e.g. u32, std_msgs.String)
49 
50  @param type: The message type
51  @type type: str
52  @return: The Rust declaration
53  @rtype: str
54  """
55  (base_type, is_array, array_len) = genmsg.msgs.parse_type(type)
56  rs_type = None
57  if (genmsg.msgs.is_builtin(base_type)):
58  rs_type = MSG_TYPE_TO_RS[base_type]
59  elif (len(base_type.split('/')) == 1):
60  if (genmsg.msgs.is_header_type(base_type)):
61  rs_type = 'super::std_msgs::Header'
62  else:
63  rs_type = base_type
64  else:
65  pkg = base_type.split('/')[0]
66  msg = base_type.split('/')[1]
67  rs_type = 'super::%s::%s' % (pkg, msg)
68 
69  if is_array:
70  if array_len is None:
71  return 'Vec<%s>' % (rs_type)
72  else:
73  return '(%s)' % (', '.join([rs_type for _ in range(array_len)]))
74  else:
75  return rs_type
76 
77 
78 def default_value(type, package):
79  """
80  Returns the value to initialize a message member with.
81 
82  0 for integer types and times,
83  0.0 for floating point,
84  false for bool,
85  default string for string,
86  ::new() for everything else
87 
88  @param type: The type
89  @type type: str
90  """
91  (base_type, is_array, array_len) = genmsg.msgs.parse_type(type)
92  if is_array and array_len is None:
93  return 'Vec::new()'
94  rs_def = None
95  rs_type = msg_type_to_rs(base_type, package)
96  if base_type in [
97  'byte', 'int8', 'int16', 'int32', 'int64', 'char', 'uint8',
98  'uint16', 'uint32', 'uint64', 'time', 'duration'
99  ]:
100  rs_def = '0' + rs_type
101  elif base_type in ['float32', 'float64']:
102  rs_def = '0.0' + rs_type
103  elif base_type == 'bool':
104  rs_def = 'false'
105  elif base_type == 'string':
106  rs_def = '::std::string::String::new()'
107  else:
108  rs_def = rs_type + '::new()'
109  if is_array and array_len is not None:
110  return '(%s)' % (', '.join([rs_def for _ in range(array_len)]))
111  else:
112  return rs_def
113 
114 
116  s = s.replace('\\', '\\\\')
117  s = s.replace('"', '\\"')
118  return s
119 
120 
122  lines = definition.splitlines()
123  if not lines:
124  lines.append('')
125  s = StringIO()
126  for line in lines:
127  line = _escape_string(line)
128  s.write('%s\\n\\\n' % (line))
129 
130  val = s.getvalue()
131  s.close()
132  return val
def escape_message_definition(definition)
Definition: __init__.py:121
def default_value(type, package)
Definition: __init__.py:78
def msg_type_to_rs(type, package)
Definition: __init__.py:45
def depending_on(spec)
Definition: __init__.py:28
def _escape_string(s)
Definition: __init__.py:115


genrs
Author(s): Adnan Ademovic
autogenerated on Mon Jun 10 2019 13:15:20