@@ -7,14 +7,40 @@ use crate::opt::GenCodeOpt;
7
7
use anyhow:: { bail, Result } ;
8
8
use fs_err as fs;
9
9
use group:: NodeGroup ;
10
+ use proc_macro2:: TokenStream ;
10
11
use quote:: quote;
11
12
use syn:: { File , Item } ;
12
13
use util:: rustfmt_string;
13
14
14
15
const INPUT_PATH : & str = "xtask/src/device_path/spec.rs" ;
15
- const OUTPUT_PATH : & str = "uefi/src/proto/device_path/device_path_gen.rs" ;
16
+ const UEFI_OUTPUT_PATH : & str = "uefi/src/proto/device_path/device_path_gen.rs" ;
16
17
17
- fn gen_code_as_string ( groups : & [ NodeGroup ] ) -> Result < String > {
18
+ fn code_to_string ( code : TokenStream ) -> Result < String > {
19
+ // Insert some blank lines to make the output a bit more readable,
20
+ // otherwise everything is entirely squished together. `rustfmt`
21
+ // doesn't currently handle inserting blank lines very well, even
22
+ // with the unstable options.
23
+ let code = code. to_string ( ) . replace ( '}' , "}\n \n " ) ;
24
+
25
+ let output = format ! (
26
+ "
27
+ // DO NOT EDIT
28
+ //
29
+ // This file was automatically generated with:
30
+ // `cargo xtask gen-code`
31
+ //
32
+ // See `/xtask/src/device_path/README.md` for more details.
33
+ #![allow(clippy::missing_const_for_fn)]
34
+
35
+ {code}"
36
+ ) ;
37
+
38
+ let formatted = rustfmt_string ( output) ?;
39
+
40
+ Ok ( formatted)
41
+ }
42
+
43
+ fn gen_uefi_code_as_string ( groups : & [ NodeGroup ] ) -> Result < String > {
18
44
let packed_modules = groups. iter ( ) . map ( NodeGroup :: gen_packed_module) ;
19
45
let node_enum = NodeGroup :: gen_node_enum ( groups) ;
20
46
let build_modules = groups. iter ( ) . map ( NodeGroup :: gen_builder_module) ;
@@ -52,28 +78,7 @@ fn gen_code_as_string(groups: &[NodeGroup]) -> Result<String> {
52
78
}
53
79
) ;
54
80
55
- // Insert some blank lines to make the output a bit more readable,
56
- // otherwise everything is entirely squished together. `rustfmt`
57
- // doesn't currently handle inserting blank lines very well, even
58
- // with the unstable options.
59
- let code = code. to_string ( ) . replace ( '}' , "}\n \n " ) ;
60
-
61
- let output = format ! (
62
- "
63
- // DO NOT EDIT
64
- //
65
- // This file was automatically generated with:
66
- // `cargo xtask gen-code`
67
- //
68
- // See `/xtask/src/device_path/README.md` for more details.
69
- #![allow(clippy::missing_const_for_fn)]
70
-
71
- {code}"
72
- ) ;
73
-
74
- let formatted = rustfmt_string ( output) ?;
75
-
76
- Ok ( formatted)
81
+ code_to_string ( code)
77
82
}
78
83
79
84
fn parse_spec ( spec_str : & str ) -> Vec < NodeGroup > {
@@ -95,14 +100,14 @@ pub fn gen_code(opt: &GenCodeOpt) -> Result<()> {
95
100
let spec_str = include_str ! ( "spec.rs" ) ;
96
101
97
102
let groups = parse_spec ( spec_str) ;
98
- let output_string = gen_code_as_string ( & groups) ?;
103
+ let uefi_output_string = gen_uefi_code_as_string ( & groups) ?;
99
104
100
105
if opt. check {
101
106
// Implementation note: we don't use `rustfmt --check` because
102
107
// it always exits zero when reading from stdin:
103
108
// https://github.com/rust-lang/rustfmt/issues/5376
104
109
105
- if output_string != fs:: read_to_string ( OUTPUT_PATH ) ? {
110
+ if uefi_output_string != fs:: read_to_string ( UEFI_OUTPUT_PATH ) ? {
106
111
bail ! ( "generated code is stale" ) ;
107
112
}
108
113
@@ -111,7 +116,7 @@ pub fn gen_code(opt: &GenCodeOpt) -> Result<()> {
111
116
bail ! ( "spec.rs needs formatting" ) ;
112
117
}
113
118
} else {
114
- fs:: write ( OUTPUT_PATH , output_string ) ?;
119
+ fs:: write ( UEFI_OUTPUT_PATH , uefi_output_string ) ?;
115
120
116
121
// Also format the input file. It's valid rust, but not included
117
122
// via `mod` anywhere, so the usual `cargo fmt --all` doesn't
0 commit comments