Skip to content

Commit 7e00099

Browse files
author
Vadim Platonov
committed
[Rust] Move dimension-related fields to a separate class
1 parent 24d781d commit 7e00099

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

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

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ private static String writeGroupEncoderTopTypes(
409409
try (Writer out = outputManager.createOutput(node.contextualName + " Encoder for fields and header"))
410410
{
411411
appendStructHeader(out, withLifetime(memberCoderType), false);
412-
final String rustCountType = rustTypeName(node.numInGroupType);
412+
final String rustCountType = rustTypeName(node.dimensionsNumInGroupType());
413413
final String contentProperty;
414414
final String contentBearingType;
415415
if (node.parent.isPresent())
@@ -453,7 +453,7 @@ private static String writeGroupEncoderTopTypes(
453453
indent(out, 1, "pub fn done_with_%s(mut self) -> CodecResult<%s> {\n",
454454
formatMethodName(node.originalName), withLifetime(afterGroupCoderType));
455455
indent(out, 2, "%s.write_at_position::<%s>(self.count_write_pos, &self.count, %s)?;\n",
456-
scratchChain, rustCountType, node.numInGroupType.size());
456+
scratchChain, rustCountType, node.dimensionsNumInGroupType().size());
457457
indent(out, 2, "Ok(%s)\n", atEndOfParent ? "self.parent" :
458458
format("%s::wrap(self.%s)", afterGroupCoderType, contentProperty));
459459
indent(out).append("}\n").append("}\n");
@@ -473,12 +473,12 @@ private static String writeGroupEncoderTopTypes(
473473
indent(out, 1, "pub fn %s_individually(mut self) -> CodecResult<%s> {\n",
474474
formatMethodName(node.originalName), withLifetime(memberCoderType));
475475
indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n",
476-
scratchChain, rustTypeName(node.blockLengthType),
477-
generateRustLiteral(node.blockLengthType, Integer.toString(node.blockLength)),
478-
node.blockLengthType.size());
476+
scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
477+
generateRustLiteral(node.dimensionsBlockLengthType(), Integer.toString(node.blockLength)),
478+
node.dimensionsBlockLengthType().size());
479479
indent(out, 2, "let count_pos = %s.pos;\n", scratchChain);
480480
indent(out, 2, "%s.write_type::<%s>(&0, %s)?; // preliminary group member count\n",
481-
scratchChain, rustCountType, node.numInGroupType.size());
481+
scratchChain, rustCountType, node.dimensionsNumInGroupType().size());
482482
indent(out, 2, "Ok(%s::new(self.%s, count_pos))\n", memberCoderType, contentProperty);
483483
indent(out, 1).append("}\n");
484484

@@ -510,11 +510,11 @@ private static void appendFixedSizeMemberGroupEncoderMethods(
510510
formatMethodName(node.originalName), rustCountType, DATA_LIFETIME, fieldsType,
511511
withLifetime(afterGroupCoderType));
512512
indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n",
513-
scratchChain, rustTypeName(node.blockLengthType),
514-
generateRustLiteral(node.blockLengthType, Integer.toString(node.blockLength)),
515-
node.blockLengthType.size());
513+
scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
514+
generateRustLiteral(node.dimensionsBlockLengthType(), Integer.toString(node.blockLength)),
515+
node.dimensionsBlockLengthType().size());
516516
indent(out, 2, "%s.write_type::<%s>(&count, %s)?; // group count\n",
517-
scratchChain, rustCountType, node.numInGroupType.size());
517+
scratchChain, rustCountType, node.dimensionsNumInGroupType().size());
518518
indent(out, 2, "let c = count as usize;\n");
519519
indent(out, 2, "let group_slice = %s.writable_slice::<%s>(c, %s)?;\n",
520520
scratchChain, fieldsType, node.blockLength);
@@ -527,15 +527,15 @@ scratchChain, rustTypeName(node.blockLengthType),
527527
formatMethodName(node.originalName), fieldsType,
528528
withLifetime(afterGroupCoderType));
529529
indent(out, 2, "%s.write_type::<%s>(&%s, %s)?; // block length\n",
530-
scratchChain, rustTypeName(node.blockLengthType),
531-
generateRustLiteral(node.blockLengthType, Integer.toString(node.blockLength)),
532-
node.blockLengthType.size());
530+
scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
531+
generateRustLiteral(node.dimensionsBlockLengthType(), Integer.toString(node.blockLength)),
532+
node.dimensionsBlockLengthType().size());
533533
indent(out, 2, "let count = s.len();\n");
534-
indent(out, 2, "if count > %s {\n", node.numInGroupType.maxValue());
534+
indent(out, 2, "if count > %s {\n", node.dimensionsNumInGroupType().maxValue());
535535
indent(out, 3).append("return Err(CodecErr::SliceIsLongerThanAllowedBySchema)\n");
536536
indent(out, 2).append("}\n");
537537
indent(out, 2, "%s.write_type::<%s>(&(count as %s), %s)?; // group count\n",
538-
scratchChain, rustCountType, rustCountType, node.numInGroupType.size());
538+
scratchChain, rustCountType, rustCountType, node.dimensionsNumInGroupType().size());
539539
indent(out, 2, "%s.write_slice_without_count::<%s>(s, %s)?;\n",
540540
scratchChain, fieldsType, node.blockLength);
541541
indent(out, 2, "Ok(%s)\n", atEndOfParent ? "self.parent" :
@@ -557,7 +557,7 @@ private static void writeGroupDecoderTopTypes(
557557
try (Writer out = outputManager.createOutput(node.contextualName + " Decoder for fields and header"))
558558
{
559559
appendStructHeader(out, withLifetime(memberDecoderType), false);
560-
final String rustCountType = rustTypeName(node.numInGroupType);
560+
final String rustCountType = rustTypeName(node.dimensionsNumInGroupType());
561561
final String contentProperty;
562562
final String contentBearingType;
563563
if (node.parent.isPresent())
@@ -618,9 +618,10 @@ private static void writeGroupDecoderTopTypes(
618618
indent(out, 1, "pub fn %s_individually(mut self) -> CodecResult<%s> {\n",
619619
formatMethodName(node.originalName), groupLevelNextDecoderType);
620620
indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n",
621-
toScratchChain(node), node.blockLengthType.size());
621+
toScratchChain(node), node.dimensionsBlockLengthType().size());
622622
indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n",
623-
toScratchChain(node), rustTypeName(node.numInGroupType), node.numInGroupType.size());
623+
toScratchChain(node), rustTypeName(node.dimensionsNumInGroupType()),
624+
node.dimensionsNumInGroupType().size());
624625
indent(out, 2).append("if count > 0 {\n");
625626
indent(out, 3, "Ok(Either::Left(%s::new(self.%s, count)))\n",
626627
memberDecoderType, contentProperty).append(INDENT).append(INDENT).append("} else {\n");
@@ -659,9 +660,10 @@ private static void appendFixedSizeMemberGroupDecoderMethods(
659660
initialNextDecoderType.startsWith("Either") ?
660661
initialNextDecoderType : withLifetime(initialNextDecoderType));
661662
indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n", toScratchChain(node),
662-
node.blockLengthType.size());
663+
node.dimensionsBlockLengthType().size());
663664
indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n",
664-
toScratchChain(node), rustTypeName(node.numInGroupType), node.numInGroupType.size());
665+
toScratchChain(node), rustTypeName(node.dimensionsNumInGroupType()),
666+
node.dimensionsNumInGroupType().size());
665667
indent(out, 2, "let s = %s.read_slice::<%s>(count as usize, %s)?;\n",
666668
toScratchChain(node), node.contextualName + "Member", node.blockLength);
667669
indent(out, 2, "Ok((s,%s))\n", atEndOfParent ? "self.parent.after_member()" :
@@ -738,10 +740,8 @@ private static List<GroupTreeNode> buildGroupTrees(
738740
final Token dimensionsToken = groupsTokens.get(i);
739741
final int groupHeaderTokenCount = dimensionsToken.componentTokenCount();
740742
final List<Token> dimensionsTokens = groupsTokens.subList(i, i + groupHeaderTokenCount);
741-
final PrimitiveType numInGroupType = findPrimitiveByTokenName(dimensionsTokens, "numInGroup");
742-
final Token blockLengthToken = findPrimitiveTokenByTokenName(dimensionsTokens, "blockLength");
743+
final GroupDimensions dimensions = GroupDimensions.ofTokens(dimensionsTokens);
743744
final int blockLength = groupToken.encodedLength();
744-
final PrimitiveType blockLengthType = blockLengthToken.encoding().primitiveType();
745745
i += groupHeaderTokenCount;
746746

747747
final List<Token> fields = new ArrayList<>();
@@ -758,8 +758,7 @@ private static List<GroupTreeNode> buildGroupTrees(
758758
parent,
759759
originalName,
760760
contextualName,
761-
numInGroupType,
762-
blockLengthType,
761+
dimensions,
763762
blockLength,
764763
fields,
765764
varDataSummaries);
@@ -791,13 +790,31 @@ private static Token findPrimitiveTokenByTokenName(final List<Token> tokens, fin
791790
throw new IllegalStateException(format("%s not specified for group", targetName));
792791
}
793792

793+
private static class GroupDimensions
794+
{
795+
final String typeName;
796+
final PrimitiveType numInGroupType;
797+
final PrimitiveType blockLengthType;
798+
799+
private GroupDimensions(String typeName, PrimitiveType numInGroupType, PrimitiveType blockLengthType) {
800+
this.typeName = typeName;
801+
this.numInGroupType = numInGroupType;
802+
this.blockLengthType = blockLengthType;
803+
}
804+
805+
public static GroupDimensions ofTokens(List<Token> dimensionsTokens) {
806+
final PrimitiveType numInGroupType = findPrimitiveByTokenName(dimensionsTokens, "numInGroup");
807+
final PrimitiveType blockLengthType = findPrimitiveByTokenName(dimensionsTokens, "blockLength");
808+
return new GroupDimensions(dimensionsTokens.get(0).name(), numInGroupType, blockLengthType);
809+
}
810+
}
811+
794812
static class GroupTreeNode
795813
{
796814
final Optional<GroupTreeNode> parent;
797815
final String originalName;
798816
final String contextualName;
799-
final PrimitiveType numInGroupType;
800-
final PrimitiveType blockLengthType;
817+
final GroupDimensions dimensions;
801818
final int blockLength;
802819
final List<Token> rawFields;
803820
final List<NamedToken> simpleNamedFields;
@@ -808,17 +825,15 @@ static class GroupTreeNode
808825
final Optional<GroupTreeNode> parent,
809826
final String originalName,
810827
final String contextualName,
811-
final PrimitiveType numInGroupType,
812-
final PrimitiveType blockLengthType,
828+
final GroupDimensions dimensions,
813829
final int blockLength,
814830
final List<Token> fields,
815831
final List<VarDataSummary> varData)
816832
{
817833
this.parent = parent;
818834
this.originalName = originalName;
819835
this.contextualName = contextualName;
820-
this.numInGroupType = numInGroupType;
821-
this.blockLengthType = blockLengthType;
836+
this.dimensions = dimensions;
822837
this.blockLength = blockLength;
823838
this.rawFields = fields;
824839
this.simpleNamedFields = NamedToken.gatherNamedNonConstantFieldTokens(fields);
@@ -849,6 +864,14 @@ boolean hasFixedSizeMembers()
849864
{
850865
return groups.isEmpty() && varData.isEmpty();
851866
}
867+
868+
public PrimitiveType dimensionsNumInGroupType() {
869+
return dimensions.numInGroupType;
870+
}
871+
872+
public PrimitiveType dimensionsBlockLengthType() {
873+
return dimensions.blockLengthType;
874+
}
852875
}
853876

854877
static class VarDataSummary

0 commit comments

Comments
 (0)