1515 */
1616package uk .co .real_logic .sbe .generation .rust ;
1717
18- import org .agrona .generation .OutputManager ;
19- import uk .co .real_logic .sbe .generation .rust .RustGenerator .CodecType ;
20-
2118import java .io .IOException ;
2219import java .io .Writer ;
2320import java .nio .ByteOrder ;
21+ import java .nio .file .Files ;
2422import java .util .ArrayList ;
25- import java .util .HashSet ;
26- import java .util .LinkedHashMap ;
2723import java .util .LinkedHashSet ;
28- import java .util .Map ;
2924
3025import static java .nio .ByteOrder .LITTLE_ENDIAN ;
3126import static uk .co .real_logic .sbe .generation .rust .RustGenerator .*;
3631 */
3732class LibRsDef
3833{
39- private final LinkedHashMap <String , HashSet <CodecType >> modules = new LinkedHashMap <>();
40- private final ArrayList <String > enumDefs = new ArrayList <>();
41- private final ArrayList <String > bitSetDefs = new ArrayList <>();
42-
43- private final OutputManager outputManager ;
34+ private final RustOutputManager outputManager ;
4435 private final ByteOrder byteOrder ;
4536
4637 /**
@@ -50,60 +41,42 @@ class LibRsDef
5041 * @param byteOrder for the Encoding.
5142 */
5243 LibRsDef (
53- final OutputManager outputManager ,
44+ final RustOutputManager outputManager ,
5445 final ByteOrder byteOrder )
5546 {
5647 this .outputManager = outputManager ;
5748 this .byteOrder = byteOrder ;
5849 }
5950
60- void addMod (final String modName , final CodecType codecType )
61- {
62- modules .computeIfAbsent (modName , __ -> new HashSet <>()).add (codecType );
63- }
64-
65- void addEnum (final String enumDef )
66- {
67- enumDefs .add (enumDef );
68- }
69-
70- void addBitSet (final String bitSetDef )
71- {
72- bitSetDefs .add (bitSetDef );
73- }
74-
7551 void generate () throws IOException
7652 {
7753 try (Writer libRs = outputManager .createOutput ("lib" ))
7854 {
7955 indent (libRs , 0 , "#![forbid(unsafe_code)]\n " );
8056 indent (libRs , 0 , "#![allow(clippy::upper_case_acronyms)]\n " );
8157 indent (libRs , 0 , "#![allow(non_camel_case_types)]\n " );
82- indent (libRs , 0 , "use core::{convert::TryInto};\n \n " );
58+ indent (libRs , 0 , "use ::core::{convert::TryInto};\n \n " );
59+
60+ final ArrayList <String > modules = new ArrayList <>();
61+ Files .walk (outputManager .getSrcDirPath ())
62+ .filter (Files ::isRegularFile )
63+ .map (path -> path .getFileName ().toString ())
64+ .filter (fileName -> fileName .endsWith (".rs" ))
65+ .filter (fileName -> !fileName .equals ("lib.rs" ))
66+ .map (fileName -> fileName .substring (0 , fileName .length () - 3 ))
67+ .forEach (modules ::add );
8368
8469 // add modules
85- for (final String mod : modules . keySet () )
70+ for (final String mod : modules )
8671 {
8772 indent (libRs , 0 , "pub mod %s;\n " , toLowerSnakeCase (mod ));
8873 }
8974 indent (libRs , 0 , "\n " );
9075
9176 // add re-export of modules
92- for (final Map . Entry < String , HashSet < CodecType >> entry : modules . entrySet () )
77+ for (final String module : modules )
9378 {
94- final String mod = entry .getKey ();
95- final HashSet <CodecType > codecTypes = entry .getValue ();
96-
97- if (codecTypes .size () == 1 )
98- {
99- indent (libRs , 0 , "pub use %s::%s::*;\n " ,
100- toLowerSnakeCase (mod ),
101- toLowerSnakeCase (codecTypes .toArray ()[0 ].toString ()));
102- }
103- else
104- {
105- indent (libRs , 0 , "pub use %s::{decoder::*, encoder::*};\n " , toLowerSnakeCase (mod ));
106- }
79+ indent (libRs , 0 , "pub use %s::*;\n " , toLowerSnakeCase (module ));
10780 }
10881 indent (libRs , 0 , "\n " );
10982
@@ -115,20 +88,6 @@ void generate() throws IOException
11588
11689 generateReadBuf (libRs , byteOrder );
11790 generateWriteBuf (libRs , byteOrder );
118-
119- // append generated enums
120- for (final String code : enumDefs )
121- {
122- libRs .append (code );
123- indent (libRs , 0 , "\n " );
124- }
125-
126- // append generated bitSets
127- for (final String code : bitSetDefs )
128- {
129- libRs .append (code );
130- indent (libRs , 0 , "\n " );
131- }
13291 }
13392 }
13493
0 commit comments