Skip to content

Commit 1c1ffe8

Browse files
xtask: Prepare gen_code to have two output files
* Rename `gen_code_as_string` to `gen_uefi_code_as_string` * Factor out part of that function to a new `code_to_string` function
1 parent fb6bb0d commit 1c1ffe8

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

xtask/src/device_path/mod.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,40 @@ use crate::opt::GenCodeOpt;
77
use anyhow::{bail, Result};
88
use fs_err as fs;
99
use group::NodeGroup;
10+
use proc_macro2::TokenStream;
1011
use quote::quote;
1112
use syn::{File, Item};
1213
use util::rustfmt_string;
1314

1415
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";
1617

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> {
1844
let packed_modules = groups.iter().map(NodeGroup::gen_packed_module);
1945
let node_enum = NodeGroup::gen_node_enum(groups);
2046
let build_modules = groups.iter().map(NodeGroup::gen_builder_module);
@@ -52,28 +78,7 @@ fn gen_code_as_string(groups: &[NodeGroup]) -> Result<String> {
5278
}
5379
);
5480

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)
7782
}
7883

7984
fn parse_spec(spec_str: &str) -> Vec<NodeGroup> {
@@ -95,14 +100,14 @@ pub fn gen_code(opt: &GenCodeOpt) -> Result<()> {
95100
let spec_str = include_str!("spec.rs");
96101

97102
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)?;
99104

100105
if opt.check {
101106
// Implementation note: we don't use `rustfmt --check` because
102107
// it always exits zero when reading from stdin:
103108
// https://github.com/rust-lang/rustfmt/issues/5376
104109

105-
if output_string != fs::read_to_string(OUTPUT_PATH)? {
110+
if uefi_output_string != fs::read_to_string(UEFI_OUTPUT_PATH)? {
106111
bail!("generated code is stale");
107112
}
108113

@@ -111,7 +116,7 @@ pub fn gen_code(opt: &GenCodeOpt) -> Result<()> {
111116
bail!("spec.rs needs formatting");
112117
}
113118
} else {
114-
fs::write(OUTPUT_PATH, output_string)?;
119+
fs::write(UEFI_OUTPUT_PATH, uefi_output_string)?;
115120

116121
// Also format the input file. It's valid rust, but not included
117122
// via `mod` anywhere, so the usual `cargo fmt --all` doesn't

0 commit comments

Comments
 (0)