Skip to content

Commit 135c9bb

Browse files
committed
clean
1 parent a7cd3d4 commit 135c9bb

File tree

4 files changed

+112
-66
lines changed

4 files changed

+112
-66
lines changed

src/main.rs

+65-18
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ use eyre::{bail, Context, OptionExt, Result};
1111
use parse::ParsedTargetInfoFile;
1212
use serde::Deserialize;
1313

14-
/// Information about a target obtained from the target_info markdown file.
15-
struct TargetDocs {
14+
/// Information about a target obtained from the markdown and rustc.
15+
struct TargetInfo {
1616
name: String,
1717
maintainers: Vec<String>,
1818
sections: Vec<(String, String)>,
1919
footnotes: Vec<String>,
20+
target_cfgs: Vec<(String, String)>,
21+
metadata: RustcTargetMetadata,
2022
}
2123

2224
/// All the sections that we want every doc page to have.
@@ -56,29 +58,48 @@ fn main() -> Result<()> {
5658
.wrap_err("failed loading target_info")?
5759
.into_iter()
5860
.map(|info| {
59-
let footnotes_used =
60-
info.footnotes.iter().map(|(target, _)| (target.clone(), false)).collect();
61-
TargetPatternEntry { info, used: false, footnotes_used }
61+
let footnotes_used = info
62+
.footnotes
63+
.keys()
64+
.map(|target| (target.clone(), false))
65+
.collect();
66+
TargetPatternEntry {
67+
info,
68+
used: false,
69+
footnotes_used,
70+
}
6271
})
6372
.collect::<Vec<_>>();
6473

6574
eprintln!("Collecting rustc information");
66-
let rustc_infos =
67-
targets.iter().map(|target| rustc_target_info(&rustc, target)).collect::<Vec<_>>();
75+
let rustc_infos = targets
76+
.iter()
77+
.map(|target| rustc_target_info(&rustc, target))
78+
.collect::<Vec<_>>();
6879

6980
let targets = targets
7081
.into_iter()
7182
.map(|target| target_doc_info(&mut info_patterns, target))
7283
.zip(rustc_infos)
84+
.map(|(md, rustc)| TargetInfo {
85+
name: md.name,
86+
maintainers: md.maintainers,
87+
sections: md.sections,
88+
footnotes: md.footnotes,
89+
target_cfgs: rustc.target_cfgs,
90+
metadata: rustc.metadata,
91+
})
7392
.collect::<Vec<_>>();
7493

7594
eprintln!("Rendering targets check_only={check_only}");
76-
let targets_dir = Path::new(output_src).join("platform-support").join("targets");
95+
let targets_dir = Path::new(output_src)
96+
.join("platform-support")
97+
.join("targets");
7798
if !check_only {
7899
std::fs::create_dir_all(&targets_dir).wrap_err("creating platform-support/targets dir")?;
79100
}
80-
for (info, rustc_info) in &targets {
81-
let doc = render::render_target_md(info, rustc_info);
101+
for info in &targets {
102+
let doc = render::render_target_md(info);
82103

83104
if !check_only {
84105
std::fs::write(targets_dir.join(format!("{}.md", info.name)), doc)
@@ -88,7 +109,10 @@ fn main() -> Result<()> {
88109

89110
for target_pattern in info_patterns {
90111
if !target_pattern.used {
91-
bail!("target pattern `{}` was never used", target_pattern.info.pattern);
112+
bail!(
113+
"target pattern `{}` was never used",
114+
target_pattern.info.pattern
115+
);
92116
}
93117

94118
for footnote_target in target_pattern.info.footnotes.keys() {
@@ -115,7 +139,15 @@ struct TargetPatternEntry {
115139
footnotes_used: HashMap<String, bool>,
116140
}
117141

118-
fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> TargetDocs {
142+
/// Information about a target obtained from the target_info markdown file.
143+
struct TargetInfoMd {
144+
name: String,
145+
maintainers: Vec<String>,
146+
sections: Vec<(String, String)>,
147+
footnotes: Vec<String>,
148+
}
149+
150+
fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> TargetInfoMd {
119151
let mut maintainers = Vec::new();
120152
let mut sections = Vec::new();
121153

@@ -128,7 +160,6 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta
128160

129161
maintainers.extend_from_slice(&target_pattern.maintainers);
130162

131-
132163
for (section_name, content) in &target_pattern.sections {
133164
if sections.iter().any(|(name, _)| name == section_name) {
134165
panic!(
@@ -139,7 +170,9 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta
139170
}
140171

141172
if let Some(target_footnotes) = target_pattern.footnotes.get(target) {
142-
target_pattern_entry.footnotes_used.insert(target.to_owned(), true);
173+
target_pattern_entry
174+
.footnotes_used
175+
.insert(target.to_owned(), true);
143176

144177
if !footnotes.is_empty() {
145178
panic!("target {target} is assigned metadata from more than one pattern");
@@ -149,7 +182,12 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta
149182
}
150183
}
151184

152-
TargetDocs { name: target.to_owned(), maintainers, sections, footnotes }
185+
TargetInfoMd {
186+
name: target.to_owned(),
187+
maintainers,
188+
sections,
189+
footnotes,
190+
}
153191
}
154192

155193
/// Information about a target obtained from rustc.
@@ -173,7 +211,7 @@ fn rustc_target_info(rustc: &Path, target: &str) -> RustcTargetInfo {
173211
.lines()
174212
.filter_map(|line| {
175213
if line.starts_with("target_") {
176-
let Some((key, value)) = line.split_once("=") else {
214+
let Some((key, value)) = line.split_once('=') else {
177215
// For example `unix`
178216
return None;
179217
};
@@ -191,12 +229,21 @@ fn rustc_target_info(rustc: &Path, target: &str) -> RustcTargetInfo {
191229

192230
let json_spec = rustc_stdout(
193231
rustc,
194-
&["-Zunstable-options", "--print", "target-spec-json", "--target", target],
232+
&[
233+
"-Zunstable-options",
234+
"--print",
235+
"target-spec-json",
236+
"--target",
237+
target,
238+
],
195239
);
196240
let spec = serde_json::from_str::<TargetJson>(&json_spec)
197241
.expect("parsing --print target-spec-json for metadata");
198242

199-
RustcTargetInfo { target_cfgs, metadata: spec.metadata }
243+
RustcTargetInfo {
244+
target_cfgs,
245+
metadata: spec.metadata,
246+
}
200247
}
201248

202249
fn rustc_stdout(rustc: &Path, args: &[&str]) -> String {

src/parse.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ fn load_single_target_info(entry: &DirEntry) -> Result<ParsedTargetInfoFile> {
7070
fn parse_file(name: &str, content: &str) -> Result<ParsedTargetInfoFile> {
7171
let mut frontmatter_splitter = content.split("---\n");
7272

73-
let frontmatter = frontmatter_splitter.nth(1).ok_or_eyre("missing frontmatter")?;
73+
let frontmatter = frontmatter_splitter
74+
.nth(1)
75+
.ok_or_eyre("missing frontmatter")?;
7476

7577
let frontmatter_line_count = frontmatter.lines().count() + 2; // 2 from ---
7678

@@ -86,7 +88,7 @@ fn parse_file(name: &str, content: &str) -> Result<ParsedTargetInfoFile> {
8688
let number = frontmatter_line_count + idx + 1; // 1 because "line numbers" are off by 1
8789
if line.starts_with("```") {
8890
in_codeblock ^= true; // toggle
89-
} else if line.starts_with("#") {
91+
} else if line.starts_with('#') {
9092
if in_codeblock {
9193
match sections.last_mut() {
9294
Some((_, content)) => {
@@ -121,7 +123,9 @@ fn parse_file(name: &str, content: &str) -> Result<ParsedTargetInfoFile> {
121123
}
122124
}
123125

124-
sections.iter_mut().for_each(|section| section.1 = section.1.trim().to_owned());
126+
sections
127+
.iter_mut()
128+
.for_each(|section| section.1 = section.1.trim().to_owned());
125129

126130
Ok(ParsedTargetInfoFile {
127131
pattern: name.to_owned(),

src/parse/tests.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::parse::Tier;
2-
31
#[test]
42
fn no_frontmatter() {
53
let name = "archlinux-unknown-linux-gnu.md"; // arch linux is an arch, right?
@@ -38,7 +36,6 @@ fn parse_correctly() {
3836
let name = "cat-unknown-linux-gnu.md";
3937
let content = r#"
4038
---
41-
tier: "1" # first-class cats
4239
maintainers: ["who maintains the cat?"]
4340
---
4441
## Requirements
@@ -59,7 +56,6 @@ But it should be possible.
5956

6057
assert_eq!(info.maintainers, vec!["who maintains the cat?"]);
6158
assert_eq!(info.pattern, name);
62-
assert_eq!(info.tier, Some(Tier::One));
6359
assert_eq!(
6460
info.sections,
6561
vec![

0 commit comments

Comments
 (0)