Skip to content

Commit b7b6c3b

Browse files
committed
Emit enum with value type alias
1 parent 20504bb commit b7b6c3b

5 files changed

Lines changed: 20 additions & 8 deletions

File tree

processor/src/main/java/online/sharedtype/processor/writer/converter/RustEnumConverter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.RequiredArgsConstructor;
44
import online.sharedtype.SharedType;
5+
import online.sharedtype.processor.context.Context;
56
import online.sharedtype.processor.domain.component.ComponentInfo;
67
import online.sharedtype.processor.domain.component.EnumValueInfo;
78
import online.sharedtype.processor.domain.def.EnumDef;
@@ -14,13 +15,15 @@
1415
import online.sharedtype.processor.writer.render.Template;
1516

1617
import online.sharedtype.processor.support.annotation.Nullable;
18+
1719
import java.util.ArrayList;
1820
import java.util.List;
1921
import java.util.Objects;
2022
import java.util.Set;
2123

2224
@RequiredArgsConstructor
2325
final class RustEnumConverter extends AbstractEnumConverter {
26+
private final Context ctx;
2427
private final TypeExpressionConverter typeExpressionConverter;
2528
private final RustMacroTraitsGenerator rustMacroTraitsGenerator;
2629

@@ -29,13 +32,15 @@ public Tuple<Template, AbstractTypeExpr> convert(TypeDef typeDef) {
2932
EnumDef enumDef = (EnumDef) typeDef;
3033

3134
String valueType = getValueTypeExpr(enumDef);
35+
boolean hasValue = valueType != null;
3236
// value can be literal or enum
3337
EnumExpr value = new EnumExpr(
3438
enumDef.simpleName(),
3539
extractEnumValues(enumDef.components()),
3640
rustMacroTraitsGenerator.generate(enumDef),
37-
valueType != null,
38-
valueType
41+
hasValue,
42+
valueType,
43+
hasValue && ctx.getProps().getRust().hasEnumValueTypeAlias() ? String.format("%sValue", enumDef.simpleName()) : null
3944
);
4045
return Tuple.of(Template.TEMPLATE_RUST_ENUM, value);
4146
}
@@ -76,7 +81,10 @@ static final class EnumExpr extends AbstractTypeExpr {
7681
final List<EnumerationExpr> enumerations;
7782
final Set<String> macroTraits;
7883
final boolean hasValue;
84+
@Nullable
7985
final String valueType;
86+
@Nullable
87+
final String valueTypeAlias;
8088

8189
String macroTraitsExpr() {
8290
return ConversionUtils.buildRustMacroTraitsExpr(macroTraits);

processor/src/main/java/online/sharedtype/processor/writer/converter/TemplateDataConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static Set<TemplateDataConverter> rust(Context ctx) {
3939
TypeExpressionConverter rustLiteralTypeExpressionConverter = TypeExpressionConverter.rustLiteral();
4040
Set<TemplateDataConverter> converters = new HashSet<>(3);
4141
converters.add(new RustStructConverter(ctx, rustTypeExpressionConverter, rustMacroTraitsGenerator));
42-
converters.add(new RustEnumConverter(rustLiteralTypeExpressionConverter, rustMacroTraitsGenerator));
42+
converters.add(new RustEnumConverter(ctx, rustLiteralTypeExpressionConverter, rustMacroTraitsGenerator));
4343
converters.add(new ConstantConverter(ctx, rustLiteralTypeExpressionConverter, SharedType.TargetType.RUST));
4444
return converters;
4545
}

processor/src/main/resources/sharedtype-default.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ sharedtype.rust.allow-deadcode=true
130130
sharedtype.rust.convert-to-snake-case=false
131131

132132
## Whether to have explicit type alias for enum value type
133-
## See document for more details
134-
sharedtype.rust.enum-value-type-alias=false
133+
## E.g. "pub type MyEnumValue = i32;" will be created for enum type "MyEnum" if its value type is "i32".
134+
## The type alias will also be used as any constant type that has the same enum type.
135+
sharedtype.rust.enum-value-type-alias=true
135136

136137
## Default type macros, comma separated
137138
sharedtype.rust.default-macros-traits=Debug

processor/src/main/resources/templates/rust/enum.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ pub enum {{name}} {
88
{{/enumerations}}
99
}
1010
{{#hasValue}}
11+
{{#valueTypeAlias}}
12+
pub type {{valueTypeAlias}} = {{{valueType}}};
13+
{{/valueTypeAlias}}
1114
impl {{name}} {
12-
pub const fn value(self) -> {{{valueType}}} {
15+
pub const fn value(self) -> {{valueTypeAlias}} {
1316
use {{name}}::*;
1417
match self {
1518
{{#enumerations}}
@@ -18,5 +21,4 @@ impl {{name}} {
1821
}
1922
}
2023
}
21-
2224
{{/hasValue}}

processor/src/test/java/online/sharedtype/processor/writer/converter/RustEnumConverterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class RustEnumConverterTest {
2626
private final ContextMocks ctxMocks = new ContextMocks();
2727
private final TypeExpressionConverter rustTypeExpressionConverter = mock(TypeExpressionConverter.class);
2828
private final RustMacroTraitsGenerator rustMacroTraitsGenerator = mock(RustMacroTraitsGenerator.class);
29-
private final RustEnumConverter converter = new RustEnumConverter(rustTypeExpressionConverter, rustMacroTraitsGenerator);
29+
private final RustEnumConverter converter = new RustEnumConverter(ctxMocks.getContext(), rustTypeExpressionConverter, rustMacroTraitsGenerator);
3030

3131
private final Config config = mock(Config.class);
3232
@BeforeEach
@@ -106,6 +106,7 @@ void convertEnumWithValues() {
106106
assertThat(model.macroTraits).containsExactly("TestMacro");
107107
assertThat(model.hasValue).isTrue();
108108
assertThat(model.valueType).isEqualTo("i32");
109+
assertThat(model.valueTypeAlias).isEqualTo("EnumAValue");
109110
assertThat(model.enumerations).satisfiesExactly(
110111
v1 -> {
111112
assertThat(v1.name).isEqualTo("Value1");

0 commit comments

Comments
 (0)