69
69
#![ recursion_limit = "128" ]
70
70
71
71
use quote:: quote;
72
- use std:: collections:: HashSet ;
72
+ use std:: collections:: BTreeSet ;
73
73
use std:: fs:: File ;
74
74
use std:: io:: { self , BufWriter , Write } ;
75
75
use std:: path:: Path ;
@@ -81,7 +81,7 @@ pub struct AtomType {
81
81
static_set_doc : Option < String > ,
82
82
macro_name : String ,
83
83
macro_doc : Option < String > ,
84
- atoms : HashSet < String > ,
84
+ atoms : BTreeSet < String > ,
85
85
}
86
86
87
87
impl AtomType {
@@ -114,7 +114,7 @@ impl AtomType {
114
114
atom_doc : None ,
115
115
static_set_doc : None ,
116
116
macro_doc : None ,
117
- atoms : HashSet :: new ( ) ,
117
+ atoms : BTreeSet :: new ( ) ,
118
118
}
119
119
}
120
120
@@ -181,6 +181,26 @@ impl AtomType {
181
181
)
182
182
}
183
183
184
+ #[ cfg( test) ]
185
+ /// Write generated code to destination [`Vec<u8>`] and return it as [`String`]
186
+ ///
187
+ /// Used mostly for testing or displaying a value.
188
+ pub fn write_to_string ( & mut self , mut destination : Vec < u8 > ) -> io:: Result < String >
189
+ {
190
+ destination. write_all (
191
+ self . to_tokens ( )
192
+ . to_string ( )
193
+ // Insert some newlines to make the generated code slightly easier to read.
194
+ . replace ( " [ \" " , "[\n \" " )
195
+ . replace ( "\" , " , "\" ,\n " )
196
+ . replace ( " ( \" " , "\n ( \" " )
197
+ . replace ( "; " , ";\n " )
198
+ . as_bytes ( ) ,
199
+ ) ?;
200
+ let str = String :: from_utf8 ( destination) . unwrap ( ) ;
201
+ Ok ( str)
202
+ }
203
+
184
204
fn to_tokens ( & mut self ) -> proc_macro2:: TokenStream {
185
205
// `impl Default for Atom` requires the empty string to be in the static set.
186
206
// This also makes sure the set in non-empty,
@@ -315,3 +335,16 @@ impl AtomType {
315
335
self . write_to ( BufWriter :: new ( File :: create ( path) ?) )
316
336
}
317
337
}
338
+
339
+ #[ test]
340
+ fn test_iteration_order ( ) {
341
+ let x1 = crate :: AtomType :: new ( "foo::Atom" , "foo_atom!" )
342
+ . atoms ( & [ "x" , "xlink" , "svg" , "test" ] )
343
+ . write_to_string ( Vec :: new ( ) ) . expect ( "write to string cache x1" ) ;
344
+
345
+ let x2 = crate :: AtomType :: new ( "foo::Atom" , "foo_atom!" )
346
+ . atoms ( & [ "x" , "xlink" , "svg" , "test" ] )
347
+ . write_to_string ( Vec :: new ( ) ) . expect ( "write to string cache x2" ) ;
348
+
349
+ assert_eq ! ( x1, x2) ;
350
+ }
0 commit comments