From 20ac5cfa4e8f70d7882d56b79047aacc442c6ec2 Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 13:16:23 +0800 Subject: [PATCH 1/8] [Rust] impl FromStr for generated enum items --- rust/tests/baseline_enum_from_str.rs | 21 +++++++++++++++++++ .../sbe/generation/rust/RustGenerator.java | 16 ++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 rust/tests/baseline_enum_from_str.rs diff --git a/rust/tests/baseline_enum_from_str.rs b/rust/tests/baseline_enum_from_str.rs new file mode 100644 index 0000000000..4e52fd4bf9 --- /dev/null +++ b/rust/tests/baseline_enum_from_str.rs @@ -0,0 +1,21 @@ +use examples_baseline::{ + boost_type::BoostType, +}; + +#[test] +fn test_boost_type_from_str() -> Result<(), ()> { + assert_eq!("TURBO".parse::()?, BoostType::TURBO, "Parse \"TURBO\" as BoostType"); + assert_eq!("SUPERCHARGER".parse::()?, BoostType::SUPERCHARGER, "Parse \"SUPERCHARGER\" as BoostType"); + assert_eq!("NITROUS".parse::()?, BoostType::NITROUS, "Parse \"NITROUS\" as BoostType"); + assert_eq!("KERS".parse::()?, BoostType::KERS, "Parse \"KERS\" as BoostType"); + + assert_eq!("Turbo".parse::()?, BoostType::NullVal, "Parse \"Turbo\" as BoostType"); + assert_eq!("Supercharger".parse::()?, BoostType::NullVal, "Parse \"Supercharger\" as BoostType"); + assert_eq!("Nitrous".parse::()?, BoostType::NullVal, "Parse \"Nitrous\" as BoostType"); + assert_eq!("Kers".parse::()?, BoostType::NullVal, "Parse \"Kers\" as BoostType"); + + assert_eq!("AA".parse::()?, BoostType::NullVal, "Parse \"AA\" as BoostType"); + assert_eq!("".parse::().unwrap(), BoostType::NullVal, "Parse \"\" as BoostType"); + + Ok(()) +} diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 5ed04fc9fb..f40cfa07d3 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1283,6 +1283,22 @@ private static void generateEnum( indent(writer, 2, "}\n"); indent(writer, 1, "}\n"); indent(writer, 0, "}\n"); + + // FromStr impl + indent(writer, 0, "impl core::str::FromStr for %s {\n", enumRustName); + indent(writer, 1, "type Err = ();\n\n"); + indent(writer, 1, "#[inline]\n"); + indent(writer, 1, "fn from_str(v: &str) -> core::result::Result {\n", primitiveType); + indent(writer, 2, "match v {\n"); + for (final Token token : messageBody) + { + indent(writer, 3, "\"%1$s\" => core::result::Result::Ok(Self::%1$s), \n", token.name()); + } + // default => NullVal + indent(writer, 3, "_ => core::result::Result::Ok(Self::NullVal),\n"); + indent(writer, 2, "}\n"); + indent(writer, 1, "}\n"); + indent(writer, 0, "}\n"); } private static void generateComposites( From cc3c466036a92353e875bbf09b304696c68922ed Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 13:35:03 +0800 Subject: [PATCH 2/8] [Rust] impl Display for generate enum items --- rust/tests/baseline_enum_to_str.rs | 14 ++++++++++++++ .../sbe/generation/rust/RustGenerator.java | 15 +++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 rust/tests/baseline_enum_to_str.rs diff --git a/rust/tests/baseline_enum_to_str.rs b/rust/tests/baseline_enum_to_str.rs new file mode 100644 index 0000000000..93f5206517 --- /dev/null +++ b/rust/tests/baseline_enum_to_str.rs @@ -0,0 +1,14 @@ +use examples_baseline::{ + boost_type::BoostType, +}; + +#[test] +fn test_boost_type_from_str() -> Result<(), ()> { + assert_eq!(format!("{}", BoostType::TURBO), "TURBO", "Display \"TURBO\""); + assert_eq!(format!("{}", BoostType::SUPERCHARGER), "SUPERCHARGER", "Display \"SUPERCHARGER\""); + assert_eq!(format!("{}", BoostType::NITROUS), "NITROUS", "Display \"NITROUS\""); + assert_eq!(format!("{}", BoostType::KERS), "KERS", "Display \"KERS\""); + assert_eq!(format!("{}", BoostType::NullVal), "NullVal", "Display \"NullVal\""); + + Ok(()) +} diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index f40cfa07d3..d062097cd1 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1299,6 +1299,21 @@ private static void generateEnum( indent(writer, 2, "}\n"); indent(writer, 1, "}\n"); indent(writer, 0, "}\n"); + + // Display impl + indent(writer, 0, "impl core::fmt::Display for %s {\n", enumRustName); + indent(writer, 1, "#[inline]\n"); + indent(writer, 1, "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n", primitiveType); + indent(writer, 2, "match self {\n"); + for (final Token token : messageBody) + { + indent(writer, 3, "Self::%1$s => write!(f, \"%1$s\"), \n", token.name()); + } + // default => Err + indent(writer, 3, "Self::NullVal => write!(f, \"NullVal\"),\n"); + indent(writer, 2, "}\n"); + indent(writer, 1, "}\n"); + indent(writer, 0, "}\n"); } private static void generateComposites( From 98fe82bb9de07ebb731578fb5277f6a35268d53f Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 13:51:50 +0800 Subject: [PATCH 3/8] [Rust] impl Into for generated enum items --- rust/tests/baseline_enum_into.rs | 14 +++++++++++++ .../sbe/generation/rust/RustGenerator.java | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 rust/tests/baseline_enum_into.rs diff --git a/rust/tests/baseline_enum_into.rs b/rust/tests/baseline_enum_into.rs new file mode 100644 index 0000000000..9580e51b0d --- /dev/null +++ b/rust/tests/baseline_enum_into.rs @@ -0,0 +1,14 @@ +use examples_baseline::{ + boost_type::BoostType, +}; + +#[test] +fn test_boost_type_from_str() -> Result<(), ()> { + assert_eq!(>::into(BoostType::TURBO), 84_u8, "BoostType::TURBO into value"); + assert_eq!(>::into(BoostType::SUPERCHARGER), 83_u8, "BoostType::SUPERCHARGER into value"); + assert_eq!(>::into(BoostType::NITROUS), 78_u8, "BoostType::NITROUS into value"); + assert_eq!(>::into(BoostType::KERS), 75_u8, "BoostType::KERS into value"); + assert_eq!(>::into(BoostType::NullVal), 0_u8, "BoostType::NullVal into value"); + + Ok(()) +} diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index d062097cd1..422491f4e0 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1284,6 +1284,27 @@ private static void generateEnum( indent(writer, 1, "}\n"); indent(writer, 0, "}\n"); + // Into impl + indent(writer, 0, "impl Into<%s> for %s {\n", primitiveType, enumRustName); + indent(writer, 1, "#[inline]\n"); + indent(writer, 1, "fn into(self) -> %s {\n", primitiveType); + indent(writer, 2, "match self {\n"); + for (final Token token : messageBody) + { + final Encoding encoding = token.encoding(); + final String literal = generateRustLiteral(encoding.primitiveType(), encoding.constValue().toString()); + indent(writer, 3, "Self::%s => %s, \n", token.name(), literal); + } + { + final Encoding encoding = messageBody.get(0).encoding(); + final CharSequence nullVal = generateRustLiteral(encoding.primitiveType(), + encoding.applicableNullValue().toString()); + indent(writer, 3, "Self::NullVal => %s,\n", nullVal); + } + indent(writer, 2, "}\n"); + indent(writer, 1, "}\n"); + indent(writer, 0, "}\n"); + // FromStr impl indent(writer, 0, "impl core::str::FromStr for %s {\n", enumRustName); indent(writer, 1, "type Err = ();\n\n"); From 6b0e9ad8197c135587732d96fcab754a2d02123d Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 14:13:29 +0800 Subject: [PATCH 4/8] [Rust] reformat code according to checkstyle failure report --- .../uk/co/real_logic/sbe/generation/rust/RustGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 422491f4e0..4a62c38def 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1298,7 +1298,7 @@ private static void generateEnum( { final Encoding encoding = messageBody.get(0).encoding(); final CharSequence nullVal = generateRustLiteral(encoding.primitiveType(), - encoding.applicableNullValue().toString()); + encoding.applicableNullValue().toString()); indent(writer, 3, "Self::NullVal => %s,\n", nullVal); } indent(writer, 2, "}\n"); From e6d47c9c65f1fe6b875d03468fbe913ba5746e3f Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 17:05:25 +0800 Subject: [PATCH 5/8] [Rust] split code in generateEnum to adress CI checkstyle method too log failure --- .../sbe/generation/rust/RustGenerator.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 4a62c38def..8d8cf9eb15 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1266,6 +1266,20 @@ private static void generateEnum( indent(writer, 0, "}\n"); // From impl + generateFromImplForEnum(enumRustName, primitiveType, messageBody, writer); + + // Into impl + generateIntoImplForEnum(enumRustName, primitiveType, messageBody, writer); + + // FromStr impl + generateFromStrImplForEnum(enumRustName, primitiveType, messageBody, writer); + + // Display impl + generateDisplayImplForEnum(enumRustName, primitiveType, messageBody, writer); + } + + private static void generateFromImplForEnum( + final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException { indent(writer, 0, "impl From<%s> for %s {\n", primitiveType, enumRustName); indent(writer, 1, "#[inline]\n"); indent(writer, 1, "fn from(v: %s) -> Self {\n", primitiveType); @@ -1283,8 +1297,10 @@ private static void generateEnum( indent(writer, 2, "}\n"); indent(writer, 1, "}\n"); indent(writer, 0, "}\n"); + } - // Into impl + private static void generateIntoImplForEnum( + final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException { indent(writer, 0, "impl Into<%s> for %s {\n", primitiveType, enumRustName); indent(writer, 1, "#[inline]\n"); indent(writer, 1, "fn into(self) -> %s {\n", primitiveType); @@ -1298,14 +1314,16 @@ private static void generateEnum( { final Encoding encoding = messageBody.get(0).encoding(); final CharSequence nullVal = generateRustLiteral(encoding.primitiveType(), - encoding.applicableNullValue().toString()); + encoding.applicableNullValue().toString()); indent(writer, 3, "Self::NullVal => %s,\n", nullVal); } indent(writer, 2, "}\n"); indent(writer, 1, "}\n"); indent(writer, 0, "}\n"); + } - // FromStr impl + private static void generateFromStrImplForEnum( + final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException{ indent(writer, 0, "impl core::str::FromStr for %s {\n", enumRustName); indent(writer, 1, "type Err = ();\n\n"); indent(writer, 1, "#[inline]\n"); @@ -1320,8 +1338,10 @@ private static void generateEnum( indent(writer, 2, "}\n"); indent(writer, 1, "}\n"); indent(writer, 0, "}\n"); + } - // Display impl + private static void generateDisplayImplForEnum( + final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException{ indent(writer, 0, "impl core::fmt::Display for %s {\n", enumRustName); indent(writer, 1, "#[inline]\n"); indent(writer, 1, "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n", primitiveType); From 2de548327e41683e7657241ea351a8f9252da30a Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 17:24:51 +0800 Subject: [PATCH 6/8] [Rust] reformat code according to checkstyle failure --- .../sbe/generation/rust/RustGenerator.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 8d8cf9eb15..0a4c85d57e 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1279,7 +1279,11 @@ private static void generateEnum( } private static void generateFromImplForEnum( - final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException { + final String enumRustName, + final String primitiveType, + final List messageBody, + final Appendable writer) throws IOException + { indent(writer, 0, "impl From<%s> for %s {\n", primitiveType, enumRustName); indent(writer, 1, "#[inline]\n"); indent(writer, 1, "fn from(v: %s) -> Self {\n", primitiveType); @@ -1300,7 +1304,11 @@ private static void generateFromImplForEnum( } private static void generateIntoImplForEnum( - final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException { + final String enumRustName, + final String primitiveType, + final List messageBody, + final Appendable writer) throws IOException + { indent(writer, 0, "impl Into<%s> for %s {\n", primitiveType, enumRustName); indent(writer, 1, "#[inline]\n"); indent(writer, 1, "fn into(self) -> %s {\n", primitiveType); @@ -1314,7 +1322,7 @@ private static void generateIntoImplForEnum( { final Encoding encoding = messageBody.get(0).encoding(); final CharSequence nullVal = generateRustLiteral(encoding.primitiveType(), - encoding.applicableNullValue().toString()); + encoding.applicableNullValue().toString()); indent(writer, 3, "Self::NullVal => %s,\n", nullVal); } indent(writer, 2, "}\n"); @@ -1323,7 +1331,11 @@ private static void generateIntoImplForEnum( } private static void generateFromStrImplForEnum( - final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException{ + final String enumRustName, + final String primitiveType, + final List messageBody, + final Appendable writer) throws IOException + { indent(writer, 0, "impl core::str::FromStr for %s {\n", enumRustName); indent(writer, 1, "type Err = ();\n\n"); indent(writer, 1, "#[inline]\n"); @@ -1341,7 +1353,11 @@ private static void generateFromStrImplForEnum( } private static void generateDisplayImplForEnum( - final String enumRustName, final String primitiveType,final List messageBody, final Appendable writer) throws IOException{ + final String enumRustName, + final String primitiveType, + final List messageBody, + final Appendable writer) throws IOException + { indent(writer, 0, "impl core::fmt::Display for %s {\n", enumRustName); indent(writer, 1, "#[inline]\n"); indent(writer, 1, "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n", primitiveType); From f96ccc63aa6f080064d0e9c666326a1b19a3c09a Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 18:03:19 +0800 Subject: [PATCH 7/8] [Rust] fix CodeQL warning by removing unused argument --- .../uk/co/real_logic/sbe/generation/rust/RustGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 0a4c85d57e..96bcf210d0 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1339,7 +1339,7 @@ private static void generateFromStrImplForEnum( indent(writer, 0, "impl core::str::FromStr for %s {\n", enumRustName); indent(writer, 1, "type Err = ();\n\n"); indent(writer, 1, "#[inline]\n"); - indent(writer, 1, "fn from_str(v: &str) -> core::result::Result {\n", primitiveType); + indent(writer, 1, "fn from_str(v: &str) -> core::result::Result {\n"); indent(writer, 2, "match v {\n"); for (final Token token : messageBody) { From 3b63ba41c5567e5344b3dfe32e942899c10bc4a7 Mon Sep 17 00:00:00 2001 From: Elvis Wang Date: Thu, 31 Oct 2024 18:45:35 +0800 Subject: [PATCH 8/8] [Rust] fix CodeQL warning by removing unused argument --- .../uk/co/real_logic/sbe/generation/rust/RustGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java index 96bcf210d0..7c2a492ce4 100644 --- a/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java +++ b/sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/rust/RustGenerator.java @@ -1360,7 +1360,7 @@ private static void generateDisplayImplForEnum( { indent(writer, 0, "impl core::fmt::Display for %s {\n", enumRustName); indent(writer, 1, "#[inline]\n"); - indent(writer, 1, "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n", primitiveType); + indent(writer, 1, "fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {\n"); indent(writer, 2, "match self {\n"); for (final Token token : messageBody) {