@@ -53,10 +53,10 @@ public RustGenerator(final Ir ir, final OutputManager outputManager)
5353
5454 public void generate () throws IOException
5555 {
56- generateSharedImports (ir , outputManager );
56+ generateSharedImports (outputManager );
5757 generateResultEnums (outputManager );
5858 generateDecoderScratchStruct (outputManager );
59- generateEncoderScratchStruct (ir , outputManager );
59+ generateEncoderScratchStruct (outputManager );
6060 generateEitherEnum (outputManager );
6161 generateEnums (ir , outputManager );
6262 generateComposites (ir , outputManager );
@@ -203,7 +203,7 @@ private static void generateSingleBitSet(final List<Token> tokens, final OutputM
203203 indent (writer , 1 , "fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {\n " );
204204 indent (writer , 2 , "write!(fmt, \" %s[" , setType );
205205
206- final StringBuilder string = new StringBuilder ();
206+ final StringBuilder builder = new StringBuilder ();
207207 final StringBuilder arguments = new StringBuilder ();
208208 for (final Token token : tokens )
209209 {
@@ -215,11 +215,11 @@ private static void generateSingleBitSet(final List<Token> tokens, final OutputM
215215 final String choiceName = formatMethodName (token .name ());
216216 final String choiceBitIndex = token .encoding ().constValue ().toString ();
217217
218- string .append (choiceName + "(" + choiceBitIndex + ")={}," );
219- arguments .append ("self.get_" + choiceName + "()," );
218+ builder .append (choiceName ). append ( "(" ). append ( choiceBitIndex ). append ( ")={}," );
219+ arguments .append ("self.get_" ). append ( choiceName ). append ( "()," );
220220 }
221221
222- writer .append (string .toString () + "]\" ,\n " );
222+ writer .append (builder .toString ()). append ( "]\" ,\n " );
223223 indent (writer , 3 , arguments .toString () + ")\n " );
224224 indent (writer , 1 , "}\n " );
225225 writer .append ("}\n " );
@@ -308,6 +308,7 @@ private static String generateFixedFieldCoder(
308308 fieldStruct .name , topType , fieldStruct .sizeBytes (),
309309 messageEncodedLength - fieldStruct .sizeBytes ());
310310 writer .append ("}\n " );
311+
311312 return decoderName ;
312313 }
313314 }
@@ -535,6 +536,9 @@ private static void appendFixedSizeMemberGroupEncoderMethods(
535536 final String fieldsType ,
536537 final String scratchChain ) throws IOException
537538 {
539+ final String s = atEndOfParent ?
540+ "self.parent" : format ("%s::wrap(self.%s)" , afterGroupCoderType , contentProperty );
541+
538542 indent (out ).append ("#[inline]\n " );
539543 indent (out , 1 , "pub fn %s_as_slice(mut self, count: %s) -> CodecResult<(&%s mut [%s], %s)> {\n " ,
540544 formatMethodName (node .originalName ), rustCountType , DATA_LIFETIME , fieldsType ,
@@ -548,8 +552,8 @@ scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
548552 indent (out , 2 , "let c = count as usize;\n " );
549553 indent (out , 2 , "let group_slice = %s.writable_slice::<%s>(c, %s)?;\n " ,
550554 scratchChain , fieldsType , node .blockLength );
551- indent ( out , 2 , "Ok((group_slice, %s)) \n " , atEndOfParent ?
552- "self.parent" : format ( "%s::wrap(self. %s)" , afterGroupCoderType , contentProperty ) );
555+
556+ indent ( out , 2 , "Ok((group_slice, %s)) \n " , s );
553557 indent (out , 1 ).append ("}\n " );
554558
555559 indent (out ).append ("#[inline]\n " );
@@ -568,8 +572,7 @@ scratchChain, rustTypeName(node.dimensionsBlockLengthType()),
568572 scratchChain , rustCountType , rustCountType , node .dimensionsNumInGroupType ().size ());
569573 indent (out , 2 , "%s.write_slice_without_count::<%s>(s, %s)?;\n " ,
570574 scratchChain , fieldsType , node .blockLength );
571- indent (out , 2 , "Ok(%s)\n " , atEndOfParent ? "self.parent" :
572- format ("%s::wrap(self.%s)" , afterGroupCoderType , contentProperty ));
575+ indent (out , 2 , "Ok(%s)\n " , s );
573576 indent (out , 1 ).append ("}\n " );
574577 }
575578
@@ -1129,7 +1132,7 @@ private static void generateEnums(final Ir ir, final OutputManager outputManager
11291132 }
11301133 }
11311134
1132- static void generateSharedImports (final Ir ir , final OutputManager outputManager ) throws IOException
1135+ static void generateSharedImports (final OutputManager outputManager ) throws IOException
11331136 {
11341137 try (Writer writer = outputManager .createOutput ("Imports core rather than std to broaden usable environments." ))
11351138 {
@@ -1155,7 +1158,7 @@ static void generateResultEnums(final OutputManager outputManager) throws IOExce
11551158 }
11561159 }
11571160
1158- static void generateEncoderScratchStruct (final Ir ir , final OutputManager outputManager ) throws IOException
1161+ static void generateEncoderScratchStruct (final OutputManager outputManager ) throws IOException
11591162 {
11601163 try (Writer writer = outputManager .createOutput ("Scratch Encoder Data Wrapper - codec internal use only" ))
11611164 {
@@ -1464,25 +1467,21 @@ private RustArrayType(final RustTypeDescriptor component, final int length)
14641467 this .length = length ;
14651468 }
14661469
1467- @ Override
14681470 public String name ()
14691471 {
14701472 return getRustStaticArrayString (componentType .name (), length );
14711473 }
14721474
1473- @ Override
14741475 public String literalValue (final String valueRep )
14751476 {
14761477 return getRustStaticArrayString (valueRep + componentType .name (), length );
14771478 }
14781479
1479- @ Override
14801480 public int sizeBytes ()
14811481 {
14821482 return componentType .sizeBytes () * length ;
14831483 }
14841484
1485- @ Override
14861485 public String defaultValue ()
14871486 {
14881487 final String defaultValue = RustTypeDescriptor .super .defaultValue ();
@@ -1504,6 +1503,7 @@ public String defaultValue()
15041503 }
15051504 }
15061505 result .append (']' );
1506+
15071507 return result .toString ();
15081508 }
15091509 }
@@ -1520,19 +1520,16 @@ private RustPrimitiveType(final String name, final int sizeBytes)
15201520 this .sizeBytes = sizeBytes ;
15211521 }
15221522
1523- @ Override
15241523 public String name ()
15251524 {
15261525 return name ;
15271526 }
15281527
1529- @ Override
15301528 public String literalValue (final String valueRep )
15311529 {
15321530 return valueRep + name ;
15331531 }
15341532
1535- @ Override
15361533 public int sizeBytes ()
15371534 {
15381535 return sizeBytes ;
@@ -1550,20 +1547,17 @@ private AnyRustType(final String name, final int sizeBytes)
15501547 this .sizeBytes = sizeBytes ;
15511548 }
15521549
1553- @ Override
15541550 public String name ()
15551551 {
15561552 return name ;
15571553 }
15581554
1559- @ Override
15601555 public String literalValue (final String valueRep )
15611556 {
15621557 final String msg = String .format ("Cannot produce a literal value %s of type %s!" , valueRep , name );
15631558 throw new UnsupportedOperationException (msg );
15641559 }
15651560
1566- @ Override
15671561 public int sizeBytes ()
15681562 {
15691563 return sizeBytes ;
@@ -1583,6 +1577,7 @@ static RustTypeDescriptor ofPrimitiveToken(final Token token)
15831577 {
15841578 return new RustArrayType (type , token .arrayLength ());
15851579 }
1580+
15861581 return type ;
15871582 }
15881583
@@ -1617,7 +1612,7 @@ private RustStruct(final String name, final List<RustStructField> fields, final
16171612
16181613 public int sizeBytes ()
16191614 {
1620- return fields .stream ().mapToInt (v -> v .type .sizeBytes ()).sum ();
1615+ return fields .stream ().mapToInt (( v ) -> v .type .sizeBytes ()).sum ();
16211616 }
16221617
16231618 static RustStruct fromHeader (final HeaderStructure header )
@@ -1626,13 +1621,15 @@ static RustStruct fromHeader(final HeaderStructure header)
16261621 final String originalTypeName = tokens .get (0 ).applicableTypeName ();
16271622 final String formattedTypeName = formatTypeName (originalTypeName );
16281623 final SplitCompositeTokens splitTokens = SplitCompositeTokens .splitInnerTokens (tokens );
1629- return RustStruct .fromTokens (formattedTypeName ,
1630- splitTokens .nonConstantEncodingTokens (),
1631- EnumSet .of (Modifier .PACKED , Modifier .DEFAULT ));
1624+
1625+ return RustStruct .fromTokens (
1626+ formattedTypeName ,
1627+ splitTokens .nonConstantEncodingTokens (),
1628+ EnumSet .of (Modifier .PACKED , Modifier .DEFAULT ));
16321629 }
16331630
1634- static RustStruct fromTokens (final String name , final List < NamedToken > tokens ,
1635- final EnumSet <Modifier > modifiers )
1631+ static RustStruct fromTokens (
1632+ final String name , final List < NamedToken > tokens , final EnumSet <Modifier > modifiers )
16361633 {
16371634 return new RustStruct (name , collectStructFields (tokens ), modifiers );
16381635 }
@@ -1656,7 +1653,7 @@ void appendDefinitionTo(final Appendable appendable) throws IOException
16561653 {
16571654 final boolean needsDefault = modifiers .contains (Modifier .DEFAULT );
16581655 final boolean canDeriveDefault = fields .stream ()
1659- .allMatch (v -> v .type .defaultValue () == RustTypeDescriptor .DEFAULT_VALUE );
1656+ .allMatch (( v ) -> v .type .defaultValue () == RustTypeDescriptor .DEFAULT_VALUE );
16601657
16611658 final Set <Modifier > modifiers = this .modifiers .clone ();
16621659 if (needsDefault && !canDeriveDefault )
@@ -1682,8 +1679,8 @@ void appendDefinitionTo(final Appendable appendable) throws IOException
16821679 }
16831680 }
16841681
1685- void appendInstanceTo (final Appendable appendable , final int indent ,
1686- final Map < String , String > values ) throws IOException
1682+ void appendInstanceTo (final Appendable appendable , final int indent , final Map < String , String > values )
1683+ throws IOException
16871684 {
16881685 indent (appendable , indent , "%s {\n " , name );
16891686 for (final RustStructField field : fields )
@@ -1702,7 +1699,6 @@ void appendInstanceTo(final Appendable appendable, final int indent,
17021699 }
17031700 indent (appendable , indent , "}\n " );
17041701 }
1705-
17061702 }
17071703
17081704 private static final class RustStructField
@@ -1778,8 +1774,7 @@ private static List<RustStructField> collectStructFields(final List<NamedToken>
17781774 break ;
17791775
17801776 default :
1781- throw new IllegalStateException (
1782- format ("Unsupported struct property from %s" , typeToken .toString ()));
1777+ throw new IllegalStateException (format ("Unsupported struct property from %s" , typeToken ));
17831778 }
17841779 }
17851780 return fields ;
@@ -1832,9 +1827,8 @@ private static void appendStructHeader(final Appendable appendable, final String
18321827 }
18331828
18341829 private static void appendStructHeader (
1835- final Appendable appendable ,
1836- final String structName ,
1837- final Set <RustStruct .Modifier > modifiers ) throws IOException
1830+ final Appendable appendable , final String structName , final Set <RustStruct .Modifier > modifiers )
1831+ throws IOException
18381832 {
18391833 if (!modifiers .isEmpty ())
18401834 {
@@ -1873,9 +1867,7 @@ private static String getRustTypeForPrimitivePossiblyArray(
18731867 }
18741868
18751869 private static void generateConstantAccessorImpl (
1876- final Appendable writer ,
1877- final String formattedTypeName ,
1878- final List <Token > unfilteredFields ) throws IOException
1870+ final Appendable writer , final String formattedTypeName , final List <Token > unfilteredFields ) throws IOException
18791871 {
18801872 writer .append (format ("%nimpl %s {\n " , formattedTypeName ));
18811873
0 commit comments