@@ -11,12 +11,6 @@ use proc_macro_error::{abort, proc_macro_error};
11
11
use quote:: quote;
12
12
use syn:: { parse_macro_input, DeriveInput } ;
13
13
14
- #[ cfg( feature = "tex" ) ]
15
- use std:: fs;
16
-
17
- #[ cfg( feature = "tex" ) ]
18
- const THIN_LINE : & str = " \\ noalign{\\ color{gray!50}\\ hrule height 0.1pt}\n " ;
19
-
20
14
fn get_bits ( expr_call : & syn:: ExprCall ) -> syn:: Expr {
21
15
if let syn:: Expr :: Path ( ep) = & * expr_call. func {
22
16
if !ep. path . is_ident ( "Bits" ) {
@@ -175,34 +169,6 @@ fn prettify_condition(cond: &syn::Expr) -> String {
175
169
. replace ( " :: " , "::" )
176
170
}
177
171
178
- fn prettify_coder ( coder : & syn:: Expr ) -> String {
179
- ( quote ! { #coder} ) . to_string ( )
180
- }
181
-
182
- #[ cfg( feature = "tex" ) ]
183
- fn prettify_type ( ty : & syn:: Type ) -> ( String , String ) {
184
- let mut ret = ( quote ! { #ty} ) . to_string ( ) . replace ( ' ' , "" ) ;
185
- if ret. starts_with ( "Option<" ) {
186
- ret = ret[ 7 ..ret. len ( ) - 1 ] . to_owned ( ) ;
187
- }
188
- if ret. starts_with ( "Vec<" ) {
189
- ret = ret[ 4 ..ret. len ( ) - 1 ] . to_owned ( ) ;
190
- }
191
- let ret_href = ret. replace ( "[" , "_" ) . replace ( "]" , "_" ) . replace ( ";" , "_" ) ;
192
- ( ret_href, ret)
193
- }
194
-
195
- #[ cfg( feature = "tex" ) ]
196
- fn prettify_default ( d : String , ty : & str ) -> String {
197
- d. replace ( & ( ty. to_owned ( ) + " :: default()" ) , "" )
198
- . replace ( " :: " , "::" )
199
- }
200
-
201
- #[ cfg( feature = "tex" ) ]
202
- fn minted ( x : & str ) -> String {
203
- "\\ mintinline[breaklines]{rust}{" . to_owned ( ) + x + "}"
204
- }
205
-
206
172
#[ derive( Debug ) ]
207
173
struct Condition {
208
174
expr : Option < syn:: Expr > ,
@@ -239,12 +205,11 @@ impl Condition {
239
205
#[ derive( Debug , Clone ) ]
240
206
struct U32 {
241
207
coder : TokenStream2 ,
242
- pretty : String ,
243
208
}
244
209
245
210
#[ derive( Debug ) ]
246
211
enum Coder {
247
- WithoutConfig ( syn :: Type ) ,
212
+ WithoutConfig ,
248
213
U32 ( U32 ) ,
249
214
Select ( Condition , U32 , U32 ) ,
250
215
Vector ( U32 , Box < Coder > ) ,
@@ -253,19 +218,9 @@ enum Coder {
253
218
impl Coder {
254
219
fn config ( & self , all_default_field : & Option < syn:: Ident > ) -> ( TokenStream2 , TokenStream2 ) {
255
220
match self {
256
- Coder :: WithoutConfig ( _) => ( quote ! { ( ) } , quote ! { ( ) } ) ,
257
- Coder :: U32 ( U32 { coder, pretty : _ } ) => ( quote ! { U32Coder } , quote ! { #coder } ) ,
258
- Coder :: Select (
259
- condition,
260
- U32 {
261
- coder : coder_true,
262
- pretty : _,
263
- } ,
264
- U32 {
265
- coder : coder_false,
266
- pretty : _,
267
- } ,
268
- ) => {
221
+ Coder :: WithoutConfig => ( quote ! { ( ) } , quote ! { ( ) } ) ,
222
+ Coder :: U32 ( U32 { coder } ) => ( quote ! { U32Coder } , quote ! { #coder } ) ,
223
+ Coder :: Select ( condition, U32 { coder : coder_true } , U32 { coder : coder_false } ) => {
269
224
let cnd = condition. get_expr ( all_default_field) . unwrap ( ) ;
270
225
(
271
226
quote ! { SelectCoder <U32Coder >} ,
@@ -274,7 +229,7 @@ impl Coder {
274
229
} ,
275
230
)
276
231
}
277
- Coder :: Vector ( U32 { coder, pretty : _ } , value_coder) => {
232
+ Coder :: Vector ( U32 { coder } , value_coder) => {
278
233
let ( ty_value_coder, value_coder) = value_coder. config ( all_default_field) ;
279
234
(
280
235
quote ! { VectorCoder <#ty_value_coder>} ,
@@ -328,10 +283,8 @@ impl Field {
328
283
abort ! ( f, "Repeated coder" ) ;
329
284
}
330
285
let coder_ast = a. parse_args :: < syn:: Expr > ( ) . unwrap ( ) ;
331
- let pretty = prettify_coder ( & coder_ast) ;
332
286
coder = Some ( Coder :: U32 ( U32 {
333
287
coder : parse_coder ( coder_ast) ,
334
- pretty,
335
288
} ) ) ;
336
289
}
337
290
Some ( "default" ) => {
@@ -383,32 +336,26 @@ impl Field {
383
336
abort ! ( f, "Repeated coder_false" ) ;
384
337
}
385
338
let coder_ast = a. parse_args :: < syn:: Expr > ( ) . unwrap ( ) ;
386
- let pretty = prettify_coder ( & coder_ast) ;
387
339
coder_false = Some ( U32 {
388
340
coder : parse_coder ( coder_ast) ,
389
- pretty,
390
341
} ) ;
391
342
}
392
343
Some ( "coder_true" ) => {
393
344
if coder_true. is_some ( ) {
394
345
abort ! ( f, "Repeated coder_true" ) ;
395
346
}
396
347
let coder_ast = a. parse_args :: < syn:: Expr > ( ) . unwrap ( ) ;
397
- let pretty = prettify_coder ( & coder_ast) ;
398
348
coder_true = Some ( U32 {
399
349
coder : parse_coder ( coder_ast) ,
400
- pretty,
401
350
} ) ;
402
351
}
403
352
Some ( "size_coder" ) => {
404
353
if size_coder. is_some ( ) {
405
354
abort ! ( f, "Repeated size_coder" ) ;
406
355
}
407
356
let coder_ast = a. parse_args :: < syn:: Expr > ( ) . unwrap ( ) ;
408
- let pretty = prettify_coder ( & coder_ast) ;
409
357
size_coder = Some ( U32 {
410
358
coder : parse_size_coder ( coder_ast) ,
411
- pretty,
412
359
} ) ;
413
360
}
414
361
Some ( "nonserialized" ) => {
@@ -457,7 +404,7 @@ impl Field {
457
404
} ;
458
405
459
406
// Assume nested field if no coder.
460
- let mut coder = coder. unwrap_or_else ( || Coder :: WithoutConfig ( f . ty . clone ( ) ) ) ;
407
+ let mut coder = coder. unwrap_or_else ( || Coder :: WithoutConfig ) ;
461
408
462
409
if let Some ( c) = size_coder {
463
410
if default. is_none ( ) {
@@ -561,158 +508,8 @@ impl Field {
561
508
}
562
509
}
563
510
}
564
-
565
- #[ cfg( feature = "tex" ) ]
566
- fn texify_coder_and_cond < F > (
567
- cond : Option < & str > ,
568
- coder : & Coder ,
569
- dfl : Option < & str > ,
570
- ident : & str ,
571
- add_row : & mut F ,
572
- ) where
573
- F : FnMut ( Option < & str > , & str , Option < & str > , & str ) ,
574
- {
575
- match & coder {
576
- Coder :: WithoutConfig ( ty) => {
577
- let ( href_ty, ty) = prettify_type ( ty) ;
578
- add_row (
579
- cond. as_deref ( ) ,
580
- & ( "\\ hyperref[hdr:" . to_owned ( ) + & href_ty + "]{" + & minted ( & ty) + "}" ) ,
581
- dfl. as_deref ( ) ,
582
- ident,
583
- ) ;
584
- }
585
- Coder :: U32 ( U32 { coder : _, pretty } ) => {
586
- add_row ( cond. as_deref ( ) , & minted ( pretty) , dfl. as_deref ( ) , ident) ;
587
- }
588
- Coder :: Select (
589
- Condition {
590
- expr : _,
591
- has_all_default : _,
592
- pretty : condition,
593
- } ,
594
- U32 {
595
- coder : _,
596
- pretty : coder_true,
597
- } ,
598
- U32 {
599
- coder : _,
600
- pretty : coder_false,
601
- } ,
602
- ) => {
603
- let cond_true = if let Some ( c) = & cond {
604
- "(" . to_owned ( ) + condition + ") && (" + c + ")"
605
- } else {
606
- condition. clone ( )
607
- } ;
608
- let cond_false = if let Some ( c) = & cond {
609
- "!(" . to_owned ( ) + condition + ") && (" + c + ")"
610
- } else {
611
- "!(" . to_owned ( ) + condition + ")"
612
- } ;
613
- add_row ( Some ( & cond_true) , & minted ( coder_true) , dfl. as_deref ( ) , ident) ;
614
- add_row (
615
- Some ( & cond_false) ,
616
- & minted ( coder_false) ,
617
- dfl. as_deref ( ) ,
618
- ident,
619
- ) ;
620
- }
621
- Coder :: Vector ( size_coder, value_coder) => {
622
- Field :: texify_coder_and_cond (
623
- cond,
624
- & Coder :: U32 ( ( * size_coder) . clone ( ) ) ,
625
- Some ( "0" ) ,
626
- & ( "num_" . to_owned ( ) + ident) ,
627
- add_row,
628
- ) ;
629
- Field :: texify_coder_and_cond (
630
- Some ( & ( "0..num_" . to_owned ( ) + ident) ) ,
631
- & * * value_coder,
632
- dfl,
633
- ident,
634
- add_row,
635
- ) ;
636
- }
637
- } ;
638
- }
639
-
640
- #[ cfg( feature = "tex" ) ]
641
- fn texify ( & self , mut row : usize ) -> String {
642
- let ident = & self . name ;
643
- let ident = & quote ! { #ident} . to_string ( ) ;
644
- let ( coder, condition) = match & self . kind {
645
- FieldKind :: Unconditional ( coder) => ( coder, None ) ,
646
- FieldKind :: Conditional ( condition, coder) => ( coder, Some ( & condition. pretty ) ) ,
647
- FieldKind :: Defaulted ( condition, coder) => ( coder, Some ( & condition. pretty ) ) ,
648
- } ;
649
- let mut ret = String :: new ( ) ;
650
- let mut add_row = |cond : Option < & str > , coder : & str , dfl : Option < & str > , ident : & str | {
651
- if row != 0 {
652
- ret += THIN_LINE ;
653
- }
654
- row += 1 ;
655
- ret += & format ! (
656
- " {} & {} & {} & {} \\ \\ \n " ,
657
- minted( cond. unwrap_or( "" ) ) ,
658
- coder,
659
- minted( dfl. unwrap_or( "" ) ) ,
660
- minted( ident)
661
- ) ;
662
- } ;
663
- let default = self . default . as_ref ( ) . map ( |d| quote ! { #d } . to_string ( ) ) ;
664
- let default = match & coder {
665
- Coder :: WithoutConfig ( ty) => {
666
- let ty = prettify_type ( ty) . 1 ;
667
- default. map ( |d| prettify_default ( d, & ty) )
668
- }
669
- Coder :: Vector ( _, _) if self . default_element . is_some ( ) => {
670
- Some ( self . default_element . as_ref ( ) . unwrap ( ) . to_string ( ) )
671
- }
672
- _ => default,
673
- } ;
674
-
675
- Field :: texify_coder_and_cond (
676
- condition. map ( String :: as_str) ,
677
- coder,
678
- default. as_deref ( ) ,
679
- ident,
680
- & mut add_row,
681
- ) ;
682
- ret
683
- }
684
- }
685
-
686
- #[ cfg( feature = "tex" ) ]
687
- fn texify ( name : & str , fields : & [ Field ] ) {
688
- let mut table = String :: new ( ) ;
689
- table += & format ! (
690
- "\\ begin{{table}}[h]\n \\ caption{{{} bundle. \\ label{{hdr:{}}}}}\n " ,
691
- name, name
692
- ) ;
693
- table += r#"
694
- \centering
695
- \begin{tabular}{>{\centering\arraybackslash}m{0.27\textwidth}>{\centering\arraybackslash}m{0.3\textwidth}>{\centering\arraybackslash}m{0.1\textwidth}>{\centering\arraybackslash}m{0.27\textwidth}}
696
- \toprule
697
- \bf condition & \bf type & \bf default & \bf name \\
698
- \midrule
699
- "# ;
700
- for ( i, f) in fields. iter ( ) . enumerate ( ) {
701
- table += & f. texify ( i) ;
702
- }
703
- table += r#"
704
- \bottomrule
705
- \end{tabular}
706
- \end{table}"# ;
707
- // TODO(veluca93): this may be problematic.
708
- fs:: create_dir_all ( "tex" ) . unwrap ( ) ;
709
- let fname = format ! ( "tex/{}.tex" , name. to_owned( ) ) ;
710
- fs:: write ( fname, table) . unwrap ( ) ;
711
511
}
712
512
713
- #[ cfg( not( feature = "tex" ) ) ]
714
- fn texify ( _: & str , _: & [ Field ] ) { }
715
-
716
513
fn derive_struct ( input : DeriveInput ) -> TokenStream2 {
717
514
let name = & input. ident ;
718
515
@@ -796,8 +593,6 @@ fn derive_struct(input: DeriveInput) -> TokenStream2 {
796
593
false => quote ! { } ,
797
594
} ;
798
595
799
- texify ( & quote ! { #name} . to_string ( ) , & fields) ;
800
-
801
596
quote ! {
802
597
#impl_default
803
598
impl crate :: headers:: encodings:: UnconditionalCoder <( ) > for #name {
@@ -820,62 +615,7 @@ fn derive_struct(input: DeriveInput) -> TokenStream2 {
820
615
}
821
616
}
822
617
823
- #[ cfg( feature = "tex" ) ]
824
- fn texify_enum ( input : & DeriveInput ) {
825
- let name = & input. ident ;
826
- let name = & quote ! { #name} . to_string ( ) ;
827
- let mut table = String :: new ( ) ;
828
- table += & format ! (
829
- "\\ begin{{table}}[h]\n \\ caption{{{} enum. \\ label{{hdr:{}}}}}\n " ,
830
- name, name
831
- ) ;
832
- table += r#"
833
- \centering
834
- \begin{tabular}{cc}
835
- \toprule
836
- \bf name & \bf value \\
837
- \midrule
838
- "# ;
839
- let data = if let syn:: Data :: Enum ( enum_data) = & input. data {
840
- enum_data
841
- } else {
842
- abort ! ( input, "derive_enum didn't get a enum" ) ;
843
- } ;
844
- let mut last_variant = -1 ;
845
- for ( row, var) in data. variants . iter ( ) . enumerate ( ) {
846
- let ident = & var. ident ;
847
- let discr = & var. discriminant ;
848
- let n = quote ! { #ident} . to_string ( ) ;
849
- let discr = if let Some ( ( _, d) ) = discr {
850
- ( quote ! { #d} ) . to_string ( ) . parse :: < i32 > ( ) . unwrap ( )
851
- } else {
852
- last_variant + 1
853
- } ;
854
- last_variant = discr;
855
- if row != 0 {
856
- table += THIN_LINE ;
857
- }
858
- table += & format ! (
859
- " {} & {} \\ \\ \n " ,
860
- & minted( & n) ,
861
- & minted( & discr. to_string( ) )
862
- ) ;
863
- }
864
- table += r#"
865
- \bottomrule
866
- \end{tabular}
867
- \end{table}"# ;
868
- // TODO(veluca93): this may be problematic.
869
- fs:: create_dir_all ( "tex" ) . unwrap ( ) ;
870
- let fname = format ! ( "tex/{}.tex" , name. to_owned( ) ) ;
871
- fs:: write ( fname, table) . unwrap ( ) ;
872
- }
873
-
874
- #[ cfg( not( feature = "tex" ) ) ]
875
- fn texify_enum ( _: & DeriveInput ) { }
876
-
877
618
fn derive_enum ( input : DeriveInput ) -> TokenStream2 {
878
- texify_enum ( & input) ;
879
619
let name = & input. ident ;
880
620
quote ! {
881
621
impl crate :: headers:: encodings:: UnconditionalCoder <U32Coder > for #name {
0 commit comments