@@ -133,11 +133,7 @@ private static Optional<FieldsRepresentationSummary> generateFieldsRepresentatio
133133 final MessageComponents components ,
134134 final OutputManager outputManager ) throws IOException
135135 {
136- final List <NamedToken > namedFieldTokens = NamedToken .gatherNamedFieldTokens (components .fields );
137- if (namedFieldTokens .isEmpty ())
138- {
139- return Optional .empty ();
140- }
136+ final List <NamedToken > namedFieldTokens = NamedToken .gatherNamedNonConstantFieldTokens (components .fields );
141137
142138 final String representationStruct = messageTypeName + "Fields" ;
143139 try (Writer writer = outputManager .createOutput (messageTypeName + " Fixed-size Fields" ))
@@ -149,12 +145,36 @@ private static Optional<FieldsRepresentationSummary> generateFieldsRepresentatio
149145 generateConstantAccessorImpl (writer , representationStruct , components .fields );
150146 }
151147
152- final int numBytes = components .fields .stream ()
153- .filter ((t ) -> !t .isConstantEncoding ())
154- .filter ((t ) -> t .signal () == ENCODING || t .signal () == BEGIN_ENUM || t .signal () == BEGIN_SET )
155- .mapToInt (Token ::encodedLength )
156- .sum ();
157-
148+ // Compute the total static size in bytes of the fields representation
149+ int numBytes = 0 ;
150+ for (int i = 0 , size = components .fields .size (); i < size ;)
151+ {
152+ final Token fieldToken = components .fields .get (i );
153+ if (fieldToken .signal () == Signal .BEGIN_FIELD )
154+ {
155+ final int fieldEnd = i + fieldToken .componentTokenCount ();
156+ if (!fieldToken .isConstantEncoding ())
157+ {
158+ for (int j = i ; j < fieldEnd ; j ++)
159+ {
160+ final Token t = components .fields .get (j );
161+ if (t .isConstantEncoding ())
162+ {
163+ continue ;
164+ }
165+ if (t .signal () == ENCODING || t .signal () == BEGIN_ENUM || t .signal () == BEGIN_SET )
166+ {
167+ numBytes += t .encodedLength ();
168+ }
169+ }
170+ }
171+ i += fieldToken .componentTokenCount ();
172+ }
173+ else
174+ {
175+ throw new IllegalStateException ("field tokens must include bounding BEGIN_FIELD and END_FIELD tokens" );
176+ }
177+ }
158178 return Optional .of (new FieldsRepresentationSummary (representationStruct , numBytes ));
159179 }
160180
@@ -847,7 +867,7 @@ static class GroupTreeNode
847867 this .blockLengthType = blockLengthType ;
848868 this .blockLength = blockLength ;
849869 this .rawFields = fields ;
850- this .simpleNamedFields = NamedToken .gatherNamedFieldTokens (fields );
870+ this .simpleNamedFields = NamedToken .gatherNamedNonConstantFieldTokens (fields );
851871 this .varData = varData ;
852872
853873 parent .ifPresent ((p ) -> p .addChild (this ));
@@ -927,7 +947,7 @@ String generateVarDataEncoder(
927947 indent (writer , 3 ).append ("return Err(CodecErr::SliceIsLongerThanAllowedBySchema)\n " );
928948 indent (writer , 2 ).append ("}\n " );
929949 indent (writer , 2 ).append ("// Write data length\n " );
930- indent (writer , 2 , "%s.write_type::<%s>(&(l as %s), %s); // group length\n " ,
950+ indent (writer , 2 , "%s.write_type::<%s>(&(l as %s), %s)? ; // group length\n " ,
931951 toScratchChain (groupDepth ), rustTypeName (this .lengthType ), rustTypeName (this .lengthType ),
932952 this .lengthType .size ());
933953 indent (writer , 2 ).append (format ("%s.write_slice_without_count::<%s>(s, %s)?;\n " ,
@@ -1548,22 +1568,27 @@ private static void generateConstantAccessorImpl(
15481568
15491569 case BEGIN_ENUM :
15501570 final String enumType = formatTypeName (signalToken .applicableTypeName ());
1551- String enumValue = null ;
1571+ final String rawConstValueName = fieldToken .encoding ().constValue ().toString ();
1572+ final int indexOfDot = rawConstValueName .indexOf ('.' );
1573+ final String constValueName = -1 == indexOfDot ?
1574+ rawConstValueName : rawConstValueName .substring (indexOfDot + 1 );
1575+ boolean foundMatchingValueName = false ;
15521576 for (int j = i ; j < unfilteredFields .size (); j ++)
15531577 {
15541578 final Token searchAhead = unfilteredFields .get (j );
1555- if (searchAhead .signal () == VALID_VALUE )
1579+ if (searchAhead .signal () == VALID_VALUE && searchAhead . name (). equals ( constValueName ) )
15561580 {
1557- enumValue = searchAhead . name () ;
1581+ foundMatchingValueName = true ;
15581582 break ;
15591583 }
15601584 }
1561- if (enumValue == null )
1585+ if (! foundMatchingValueName )
15621586 {
1563- throw new IllegalStateException ("Found a constant enum field with incomplete token content" );
1587+ throw new IllegalStateException (format ("Found a constant enum field that requested value %s, " +
1588+ "which is not an available enum option." , rawConstValueName ));
15641589 }
15651590 constantRustTypeName = enumType ;
1566- constantRustExpression = enumType + "::" + enumValue ;
1591+ constantRustExpression = enumType + "::" + constValueName ;
15671592 break ;
15681593
15691594 case BEGIN_SET :
0 commit comments