Skip to content

Commit cc3c466

Browse files
committed
[Rust] impl Display for generate enum items
1 parent 20ac5cf commit cc3c466

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

rust/tests/baseline_enum_to_str.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use examples_baseline::{
2+
boost_type::BoostType,
3+
};
4+
5+
#[test]
6+
fn test_boost_type_from_str() -> Result<(), ()> {
7+
assert_eq!(format!("{}", BoostType::TURBO), "TURBO", "Display \"TURBO\"");
8+
assert_eq!(format!("{}", BoostType::SUPERCHARGER), "SUPERCHARGER", "Display \"SUPERCHARGER\"");
9+
assert_eq!(format!("{}", BoostType::NITROUS), "NITROUS", "Display \"NITROUS\"");
10+
assert_eq!(format!("{}", BoostType::KERS), "KERS", "Display \"KERS\"");
11+
assert_eq!(format!("{}", BoostType::NullVal), "NullVal", "Display \"NullVal\"");
12+
13+
Ok(())
14+
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,21 @@ private static void generateEnum(
12991299
indent(writer, 2, "}\n");
13001300
indent(writer, 1, "}\n");
13011301
indent(writer, 0, "}\n");
1302+
1303+
// Display impl
1304+
indent(writer, 0, "impl core::fmt::Display for %s {\n", enumRustName);
1305+
indent(writer, 1, "#[inline]\n");
1306+
indent(writer, 1, "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n", primitiveType);
1307+
indent(writer, 2, "match self {\n");
1308+
for (final Token token : messageBody)
1309+
{
1310+
indent(writer, 3, "Self::%1$s => write!(f, \"%1$s\"), \n", token.name());
1311+
}
1312+
// default => Err
1313+
indent(writer, 3, "Self::NullVal => write!(f, \"NullVal\"),\n");
1314+
indent(writer, 2, "}\n");
1315+
indent(writer, 1, "}\n");
1316+
indent(writer, 0, "}\n");
13021317
}
13031318

13041319
private static void generateComposites(

0 commit comments

Comments
 (0)