Skip to content

Commit 8624394

Browse files
author
Vadim Platonov
committed
[Rust] Generate NullVal enum values
Mimic the Java generator in generating null enum values. The `NullVal` values are generated even if the enum type is not optional.
1 parent 0517c5e commit 8624394

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,16 @@ private static void generateEnum(final List<Token> enumTokens, final OutputManag
13561356
.append(",\n");
13571357
}
13581358

1359+
// null value
1360+
final Token token = messageBody.get(0);
1361+
final Encoding encoding = token.encoding();
1362+
final CharSequence nullVal = generateRustLiteral(
1363+
encoding.primitiveType(), encoding.applicableNullValue().toString());
1364+
indent(writer, 1).append("NullVal")
1365+
.append(" = ")
1366+
.append(nullVal)
1367+
.append(",\n");
1368+
13591369
writer.append("}\n");
13601370
}
13611371
}

sbe-tool/src/test/java/uk/co/real_logic/sbe/generation/rust/RustGeneratorTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private void assertContainsNumericEnum(final String generatedRust)
124124
"pub enum ENUM {\n" +
125125
" Value1 = 1u8,\n" +
126126
" Value10 = 10u8,\n" +
127+
" NullVal = 255u8,\n" +
127128
"}\n";
128129
assertTrue(generatedRust.contains(expectedDeclaration));
129130
}
@@ -144,6 +145,7 @@ public void fullGenerateBroadUseCase() throws IOException, InterruptedException
144145
"pub enum BooleanType {\n" +
145146
" F = 0u8,\n" +
146147
" T = 1u8,\n" +
148+
" NullVal = 255u8,\n" +
147149
"}\n";
148150
assertTrue(generatedRust.contains(expectedBooleanTypeDeclaration));
149151
final String expectedCharTypeDeclaration =
@@ -153,6 +155,7 @@ public void fullGenerateBroadUseCase() throws IOException, InterruptedException
153155
" A = 65i8,\n" +
154156
" B = 66i8,\n" +
155157
" C = 67i8,\n" +
158+
" NullVal = 0i8,\n" +
156159
"}\n";
157160
assertTrue(generatedRust.contains(expectedCharTypeDeclaration));
158161
assertRustBuildable(generatedRust, Optional.of("example-schema"));
@@ -283,6 +286,7 @@ public void constantEnumFields() throws IOException, InterruptedException
283286
" A = 65i8,\n" +
284287
" B = 66i8,\n" +
285288
" C = 67i8,\n" +
289+
" NullVal = 0i8,\n" +
286290
"}\n";
287291
assertContains(rust, expectedCharTypeDeclaration);
288292
assertContains(rust, "pub struct ConstantEnumsFields {\n}");
@@ -313,6 +317,7 @@ public void constantFieldsCase() throws IOException, InterruptedException
313317
" A = 65i8,\n" +
314318
" B = 66i8,\n" +
315319
" C = 67i8,\n" +
320+
" NullVal = 0i8,\n" +
316321
"}\n";
317322
assertContains(rust, expectedCharTypeDeclaration);
318323
final String expectedComposite =

0 commit comments

Comments
 (0)