Skip to content

Commit

Permalink
Use syn + prettyplease to format
Browse files Browse the repository at this point in the history
This replaces the dependency on the rustfmt executable with an actual
tracked build dependency. Since we have to parse with syn anyway now, I
also took the opportunity to swap the string replacement for a
syntax-tree based replacement.
  • Loading branch information
LuigiPiucco committed Jan 12, 2025
1 parent c7c685b commit acabd4a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ optional = true
svd2rust = "0.35.0"
svdtools = "0.4.0"
atdf2svd = "0.4.0"
prettyplease = "0.2"
syn = { version = "2", default-features = false, features = ["full", "parsing"] }

[dev-dependencies]
nb = "0.1.2"
Expand Down
33 changes: 13 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::{
fs::{self, File},
io::{self, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
};
use svdtools::patch;
use syn::Item;

fn main() {
println!("cargo::rustc-check-cfg=cfg(single_mcu)");
Expand Down Expand Up @@ -135,26 +135,19 @@ _include:
svd2rust_config.interrupt_link_section = Some(".text.vector".to_owned());
let generated = svd2rust::generate(&svd_content, &svd2rust_config).unwrap();

// HACK: Using simple pattern replacement like this is likely to break
// with any formatting changes in the input. Ideally we'd parse it somehow,
// but that'd increase compile time. Perhaps reopening the file after
// rustfmt runs and patching that would be less fickle.
let pat = "# [no_mangle] static mut DEVICE_PERIPHERALS : bool = false ;";
let lib_rs = generated
.lib_rs
.replace(pat, "use super::DEVICE_PERIPHERALS;");
let module_file = pac_dir.join(mcu).with_extension("rs");
fs::write(&module_file, lib_rs).unwrap();

let status = Command::new("rustfmt")
.arg(module_file.to_str().unwrap())
.stdin(Stdio::null())
.stdout(Stdio::null())
.status()
.unwrap();
if let Err(what) = status.exit_ok() {
panic!("rustfmt failed to format generated source with: {}", what);
let mut syntax_tree = syn::parse_file(&generated.lib_rs).unwrap();
for item in syntax_tree.items.iter_mut().rev() {
{
let Item::Static(statik) = item else { continue };
if &statik.ident.to_string() != "DEVICE_PERIPHERALS" {
continue;
}
}
*item = syn::parse_quote! {use super::DEVICE_PERIPHERALS;};
}
let formatted = prettyplease::unparse(&syntax_tree);
let module_file = pac_dir.join(mcu).with_extension("rs");
fs::write(&module_file, &formatted).unwrap();
}

fn ensure_directory(dir: &Path) {
Expand Down

0 comments on commit acabd4a

Please sign in to comment.