From 9b033292608973651f6de3fa313bf920e8480150 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 16 Mar 2024 14:09:27 +0100 Subject: [PATCH] things --- .github/workflows/deploy.yml | 25 ----------------- src/main.rs | 52 ++++++++---------------------------- src/parse.rs | 44 ++++++++++++++---------------- src/parse/tests.rs | 23 ++++++++++++++++ src/render.rs | 14 +++------- 5 files changed, 58 insertions(+), 100 deletions(-) delete mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index 75917fa..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Deploy mdBook -on: [push] -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Setup mdBook - uses: peaceiris/actions-mdbook@v1 - with: - mdbook-version: '0.4.35' - - uses: dtolnay/rust-toolchain@stable - - name: Build docs - run: | - export RUSTC=$(rustup which rustc) - cargo run - - run: | - cd targets && mdbook build - - name: Deploy - uses: peaceiris/actions-gh-pages@v3 - # the build has been made rust-lang/rust specific - if: ${{ false && github.ref == 'refs/heads/master' }} - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./targets/book diff --git a/src/main.rs b/src/main.rs index 1c0c288..24aac6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,24 +58,15 @@ fn main() -> Result<()> { .wrap_err("failed loading target_info")? .into_iter() .map(|info| { - let footnotes_used = info - .footnotes - .keys() - .map(|target| (target.clone(), false)) - .collect(); - TargetPatternEntry { - info, - used: false, - footnotes_used, - } + let footnotes_used = + info.footnotes.keys().map(|target| (target.clone(), false)).collect(); + TargetPatternEntry { info, used: false, footnotes_used } }) .collect::>(); eprintln!("Collecting rustc information"); - let rustc_infos = targets - .iter() - .map(|target| rustc_target_info(&rustc, target)) - .collect::>(); + let rustc_infos = + targets.iter().map(|target| rustc_target_info(&rustc, target)).collect::>(); let targets = targets .into_iter() @@ -92,9 +83,7 @@ fn main() -> Result<()> { .collect::>(); eprintln!("Rendering targets check_only={check_only}"); - let targets_dir = Path::new(output_src) - .join("platform-support") - .join("targets"); + let targets_dir = Path::new(output_src).join("platform-support").join("targets"); if !check_only { std::fs::create_dir_all(&targets_dir).wrap_err("creating platform-support/targets dir")?; } @@ -109,10 +98,7 @@ fn main() -> Result<()> { for target_pattern in info_patterns { if !target_pattern.used { - bail!( - "target pattern `{}` was never used", - target_pattern.info.pattern - ); + bail!("target pattern `{}` was never used", target_pattern.info.pattern); } for footnote_target in target_pattern.info.footnotes.keys() { @@ -170,9 +156,7 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta } if let Some(target_footnotes) = target_pattern.footnotes.get(target) { - target_pattern_entry - .footnotes_used - .insert(target.to_owned(), true); + target_pattern_entry.footnotes_used.insert(target.to_owned(), true); if !footnotes.is_empty() { panic!("target {target} is assigned metadata from more than one pattern"); @@ -182,12 +166,7 @@ fn target_doc_info(info_patterns: &mut [TargetPatternEntry], target: &str) -> Ta } } - TargetInfoMd { - name: target.to_owned(), - maintainers, - sections, - footnotes, - } + TargetInfoMd { name: target.to_owned(), maintainers, sections, footnotes } } /// Information about a target obtained from rustc. @@ -229,21 +208,12 @@ fn rustc_target_info(rustc: &Path, target: &str) -> RustcTargetInfo { let json_spec = rustc_stdout( rustc, - &[ - "-Zunstable-options", - "--print", - "target-spec-json", - "--target", - target, - ], + &["-Zunstable-options", "--print", "target-spec-json", "--target", target], ); let spec = serde_json::from_str::(&json_spec) .expect("parsing --print target-spec-json for metadata"); - RustcTargetInfo { - target_cfgs, - metadata: spec.metadata, - } + RustcTargetInfo { target_cfgs, metadata: spec.metadata } } fn rustc_stdout(rustc: &Path, args: &[&str]) -> String { diff --git a/src/parse.rs b/src/parse.rs index dd03f28..b771c88 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -70,9 +70,7 @@ fn load_single_target_info(entry: &DirEntry) -> Result { fn parse_file(name: &str, content: &str) -> Result { let mut frontmatter_splitter = content.split("---\n"); - let frontmatter = frontmatter_splitter - .nth(1) - .ok_or_eyre("missing frontmatter")?; + let frontmatter = frontmatter_splitter.nth(1).ok_or_eyre("missing frontmatter")?; let frontmatter_line_count = frontmatter.lines().count() + 2; // 2 from --- @@ -86,20 +84,27 @@ fn parse_file(name: &str, content: &str) -> Result { for (idx, line) in body.lines().enumerate() { let number = frontmatter_line_count + idx + 1; // 1 because "line numbers" are off by 1 + + let push_line = |sections: &mut Vec<(String, String)>, line| { + match sections.last_mut() { + Some((_, content)) => { + content.push_str(line); + content.push('\n'); + } + None if line.trim().is_empty() => {} + None => { + bail!("line {number} with content not allowed before the first heading") + } + } + Ok(()) + }; + if line.starts_with("```") { in_codeblock ^= true; // toggle + push_line(&mut sections, line)?; } else if line.starts_with('#') { if in_codeblock { - match sections.last_mut() { - Some((_, content)) => { - content.push_str(line); - content.push('\n'); - } - None if line.trim().is_empty() => {} - None => { - bail!("line {number} with content not allowed before the first heading") - } - } + push_line(&mut sections, line)?; } else if let Some(header) = line.strip_prefix("## ") { if !crate::SECTIONS.contains(&header) { bail!( @@ -112,20 +117,11 @@ fn parse_file(name: &str, content: &str) -> Result { bail!("on line {number}, the only allowed headings are `## `: `{line}`"); } } else { - match sections.last_mut() { - Some((_, content)) => { - content.push_str(line); - content.push('\n'); - } - None if line.trim().is_empty() => {} - None => bail!("line with content not allowed before the first heading"), - } + push_line(&mut sections, line)?; } } - sections - .iter_mut() - .for_each(|section| section.1 = section.1.trim().to_owned()); + sections.iter_mut().for_each(|section| section.1 = section.1.trim().to_owned()); Ok(ParsedTargetInfoFile { pattern: name.to_owned(), diff --git a/src/parse/tests.rs b/src/parse/tests.rs index 6eb3593..5b0530b 100644 --- a/src/parse/tests.rs +++ b/src/parse/tests.rs @@ -74,3 +74,26 @@ But it should be possible. ] ); } + +#[test] +fn backticks() { + let name = "microservices-unknown-linux-gnu"; // microservices are my favourite architecture + let content = r#" +--- +--- + +## Testing + +```text +# hello world +``` + "#; + + let info = super::parse_file(name, content).unwrap(); + + assert_eq!(info.pattern, name); + assert_eq!( + info.sections, + vec![("Testing".to_owned(), "```text\n# hello world\n```".to_owned(),),] + ); +} diff --git a/src/render.rs b/src/render.rs index a28ffdb..ecb1804 100644 --- a/src/render.rs +++ b/src/render.rs @@ -60,10 +60,7 @@ pub fn render_target_md(target: &TargetInfo) -> String { section("Maintainers", &maintainers_content); for section_name in crate::SECTIONS { - let value = target - .sections - .iter() - .find(|(name, _)| name == section_name); + let value = target.sections.iter().find(|(name, _)| name == section_name); let section_content = match value { Some((_, value)) => value.clone(), @@ -137,12 +134,9 @@ pub fn render_static(check_only: bool, src_output: &Path, targets: &[TargetInfo] let summary = src_output.join("SUMMARY.md"); let summary_old = fs::read_to_string(&summary).wrap_err("reading SUMMARY.md")?; // indent the list - let summary_new = replace_section( - &summary_old, - "TARGET_LIST", - &target_list.replace("- ", " - "), - ) - .wrap_err("replacig SUMMARY.md")?; + let summary_new = + replace_section(&summary_old, "TARGET_LIST", &target_list.replace("- ", " - ")) + .wrap_err("replacig SUMMARY.md")?; if !check_only { fs::write(summary, summary_new).wrap_err("writing SUMAMRY.md")?; }