Skip to content

Commit

Permalink
[Rust] codegen of primitive enums now implement 'From' instead of 'In…
Browse files Browse the repository at this point in the history
…to' (#1029)

* [Rust] updated LibRsDef.generate() to add "#![allow(clippy::all)]"

* [Rust] generating 'From<ENUM> for PRIMITIVE' instead of 'Into<PRIMITIVE> for ENUM' to satisfy 'from_over_into' lint

* [Rust] removed unused method parameter

* fixed formatting

---------

Co-authored-by: Michael Ward <mward@drw.com>
  • Loading branch information
mward and Michael Ward authored Nov 28, 2024
1 parent 8af808c commit 4547fe6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void generate(final Ir ir) throws IOException
try (Writer libRs = outputManager.createOutput("lib"))
{
indent(libRs, 0, "#![forbid(unsafe_code)]\n");
indent(libRs, 0, "#![allow(clippy::upper_case_acronyms)]\n");
indent(libRs, 0, "#![allow(clippy::all)]\n");
indent(libRs, 0, "#![allow(non_camel_case_types)]\n\n");
indent(libRs, 0, "#![allow(ambiguous_glob_reexports)]\n\n");
indent(libRs, 0, "use ::core::{convert::TryInto};\n\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,19 +1265,19 @@ private static void generateEnum(
indent(writer, 0, "}\n");

// From impl
generateFromImplForEnum(enumRustName, primitiveType, messageBody, writer);
generateFromPrimitiveForEnum(enumRustName, primitiveType, messageBody, writer);

// Into impl
generateIntoImplForEnum(enumRustName, primitiveType, messageBody, writer);
generateFromEnumForPrimitive(enumRustName, primitiveType, messageBody, writer);

// FromStr impl
generateFromStrImplForEnum(enumRustName, primitiveType, messageBody, writer);
generateFromStrImplForEnum(enumRustName, messageBody, writer);

// Display impl
generateDisplayImplForEnum(enumRustName, primitiveType, messageBody, writer);
}

private static void generateFromImplForEnum(
private static void generateFromPrimitiveForEnum(
final String enumRustName,
final String primitiveType,
final List<Token> messageBody,
Expand All @@ -1302,27 +1302,29 @@ private static void generateFromImplForEnum(
indent(writer, 0, "}\n");
}

private static void generateIntoImplForEnum(
private static void generateFromEnumForPrimitive(
final String enumRustName,
final String primitiveType,
final List<Token> messageBody,
final Appendable writer) throws IOException
{
indent(writer, 0, "impl Into<%s> for %s {\n", primitiveType, enumRustName);
indent(writer, 0, "impl From<%s> for %s {\n", enumRustName, primitiveType);
indent(writer, 1, "#[inline]\n");
indent(writer, 1, "fn into(self) -> %s {\n", primitiveType);
indent(writer, 2, "match self {\n");
indent(writer, 1, "fn from(v: %s) -> Self {\n", enumRustName);
indent(writer, 2, "match v {\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);
indent(writer, 3, "%s::%s => %s, \n", enumRustName, 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, 3, "%s::NullVal => %s,\n", enumRustName, nullVal);
}
indent(writer, 2, "}\n");
indent(writer, 1, "}\n");
Expand All @@ -1331,7 +1333,6 @@ private static void generateIntoImplForEnum(

private static void generateFromStrImplForEnum(
final String enumRustName,
final String primitiveType,
final List<Token> messageBody,
final Appendable writer) throws IOException
{
Expand All @@ -1342,10 +1343,10 @@ private static void generateFromStrImplForEnum(
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());
indent(writer, 3, "\"%1$s\" => Ok(Self::%1$s), \n", token.name());
}
// default => NullVal
indent(writer, 3, "_ => core::result::Result::Ok(Self::NullVal),\n");
indent(writer, 3, "_ => Ok(Self::NullVal),\n");
indent(writer, 2, "}\n");
indent(writer, 1, "}\n");
indent(writer, 0, "}\n");
Expand Down

0 comments on commit 4547fe6

Please sign in to comment.