MCPcopy Create free account
hub / github.com/apache/fory / generate_debug_impl

Method generate_debug_impl

compiler/fory_compiler/generators/rust.py:1229–1271  ·  view source on GitHub ↗

Generate a Debug impl that avoids recursive ref expansion.

(self, message: Message)

Source from the content-addressed store, hash-verified

1227 return f'"{escaped}"'
1228
1229 def generate_debug_impl(self, message: Message) -> List[str]:
1230 """Generate a Debug impl that avoids recursive ref expansion."""
1231 lines: List[str] = []
1232 type_name = self.get_type_identifier(message)
1233 lines.append(f"impl ::std::fmt::Debug for {type_name} {{")
1234 lines.append(
1235 " fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {"
1236 )
1237 if not message.fields:
1238 lines.append(
1239 f" f.write_str({self.rust_string_literal(type_name + ' {}')})"
1240 )
1241 lines.append(" }")
1242 lines.append("}")
1243 return lines
1244 lines.append(
1245 f" f.write_str({self.rust_string_literal(type_name + ' { ')})?;"
1246 )
1247 lineage = self._lineage_for_message(message)
1248 for i, field in enumerate(message.fields):
1249 field_name = self.get_field_identifier(message, field)
1250 if i > 0:
1251 lines.append(' f.write_str(", ")?;')
1252 lines.append(
1253 f" f.write_str({self.rust_string_literal(field_name + ': ')})?;"
1254 )
1255 if self.field_needs_safe_debug(field, lineage):
1256 placeholder = f"{self.format_idl_type(field.field_type)}(...)"
1257 placeholder_literal = self.rust_string_literal(placeholder)
1258 if field.optional:
1259 lines.append(f" if self.{field_name}.is_some() {{")
1260 lines.append(f" f.write_str({placeholder_literal})?;")
1261 lines.append(" } else {")
1262 lines.append(' f.write_str("None")?;')
1263 lines.append(" }")
1264 else:
1265 lines.append(f" f.write_str({placeholder_literal})?;")
1266 else:
1267 lines.append(f' write!(f, "{{:?}}", self.{field_name})?;')
1268 lines.append(' f.write_str(" }")')
1269 lines.append(" }")
1270 lines.append("}")
1271 return lines
1272
1273 def generate_nested_module(
1274 self,

Callers 1

generate_messageMethod · 0.95

Calls 7

get_type_identifierMethod · 0.95
rust_string_literalMethod · 0.95
_lineage_for_messageMethod · 0.95
get_field_identifierMethod · 0.95
format_idl_typeMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected