Skip to content

Commit 5f0a7ba

Browse files
author
Vadim Platonov
committed
[Rust] Decode non-standard group dimensions
1 parent 7e00099 commit 5f0a7ba

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -617,13 +617,12 @@ private static void writeGroupDecoderTopTypes(
617617

618618
indent(out, 1, "pub fn %s_individually(mut self) -> CodecResult<%s> {\n",
619619
formatMethodName(node.originalName), groupLevelNextDecoderType);
620-
indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n",
621-
toScratchChain(node), node.dimensionsBlockLengthType().size());
622-
indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n",
623-
toScratchChain(node), rustTypeName(node.dimensionsNumInGroupType()),
624-
node.dimensionsNumInGroupType().size());
625-
indent(out, 2).append("if count > 0 {\n");
626-
indent(out, 3, "Ok(Either::Left(%s::new(self.%s, count)))\n",
620+
indent(out, 2, "let dim = %s.read_type::<%s>(%s)?;\n",
621+
toScratchChain(node),
622+
formatTypeName(node.dimensionsTypeName()),
623+
node.dimensionsTypeSize());
624+
indent(out, 2).append("if dim.num_in_group > 0 {\n");
625+
indent(out, 3, "Ok(Either::Left(%s::new(self.%s, dim.num_in_group)))\n",
627626
memberDecoderType, contentProperty).append(INDENT).append(INDENT).append("} else {\n");
628627

629628
if (atEndOfParent)
@@ -659,12 +658,11 @@ private static void appendFixedSizeMemberGroupDecoderMethods(
659658
formatMethodName(node.originalName), DATA_LIFETIME, node.contextualName + "Member",
660659
initialNextDecoderType.startsWith("Either") ?
661660
initialNextDecoderType : withLifetime(initialNextDecoderType));
662-
indent(out, 2, "%s.skip_bytes(%s)?; // Skip reading block length for now\n", toScratchChain(node),
663-
node.dimensionsBlockLengthType().size());
664-
indent(out, 2, "let count = *%s.read_type::<%s>(%s)?;\n",
665-
toScratchChain(node), rustTypeName(node.dimensionsNumInGroupType()),
666-
node.dimensionsNumInGroupType().size());
667-
indent(out, 2, "let s = %s.read_slice::<%s>(count as usize, %s)?;\n",
661+
indent(out, 2, "let dim = %s.read_type::<%s>(%s)?;\n",
662+
toScratchChain(node),
663+
formatTypeName(node.dimensionsTypeName()),
664+
node.dimensionsTypeSize());
665+
indent(out, 2, "let s = %s.read_slice::<%s>(dim.num_in_group as usize, %s)?;\n",
668666
toScratchChain(node), node.contextualName + "Member", node.blockLength);
669667
indent(out, 2, "Ok((s,%s))\n", atEndOfParent ? "self.parent.after_member()" :
670668
format("%s::wrap(self.%s)", initialNextDecoderType, contentProperty));
@@ -793,19 +791,25 @@ private static Token findPrimitiveTokenByTokenName(final List<Token> tokens, fin
793791
private static class GroupDimensions
794792
{
795793
final String typeName;
794+
final int typeSize;
796795
final PrimitiveType numInGroupType;
797796
final PrimitiveType blockLengthType;
798797

799-
private GroupDimensions(String typeName, PrimitiveType numInGroupType, PrimitiveType blockLengthType) {
798+
private GroupDimensions(String typeName, int typeSize,
799+
PrimitiveType numInGroupType, PrimitiveType blockLengthType)
800+
{
800801
this.typeName = typeName;
802+
this.typeSize = typeSize;
801803
this.numInGroupType = numInGroupType;
802804
this.blockLengthType = blockLengthType;
803805
}
804806

805-
public static GroupDimensions ofTokens(List<Token> dimensionsTokens) {
807+
public static GroupDimensions ofTokens(List<Token> dimensionsTokens)
808+
{
806809
final PrimitiveType numInGroupType = findPrimitiveByTokenName(dimensionsTokens, "numInGroup");
807810
final PrimitiveType blockLengthType = findPrimitiveByTokenName(dimensionsTokens, "blockLength");
808-
return new GroupDimensions(dimensionsTokens.get(0).name(), numInGroupType, blockLengthType);
811+
return new GroupDimensions(dimensionsTokens.get(0).name(), dimensionsTokens.get(0).encodedLength(),
812+
numInGroupType, blockLengthType);
809813
}
810814
}
811815

@@ -872,6 +876,14 @@ public PrimitiveType dimensionsNumInGroupType() {
872876
public PrimitiveType dimensionsBlockLengthType() {
873877
return dimensions.blockLengthType;
874878
}
879+
880+
public String dimensionsTypeName() {
881+
return dimensions.typeName;
882+
}
883+
884+
public int dimensionsTypeSize() {
885+
return dimensions.typeSize;
886+
}
875887
}
876888

877889
static class VarDataSummary

0 commit comments

Comments
 (0)