Skip to content

Commit 0c67e01

Browse files
committed
Use rust-format for generated files
Re libp2p#49
1 parent dda22c7 commit 0c67e01

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

node/lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ snark = { path = "../snark" }
2222

2323
[build-dependencies]
2424
regex = "1"
25+
rust-format = "0.3"

node/lib/build.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ fn main() -> Result<(), Box<dyn Error>> {
6767
let Some(line) = lines.next() else { break };
6868
let line = line.unwrap();
6969

70-
let Some(matches) = action_def_re.captures(&line) else { continue };
70+
let Some(matches) = action_def_re.captures(&line) else {
71+
continue;
72+
};
7173
match &matches[1] {
7274
"struct" => {
7375
if let Some(action_name) = matches.get(2) {
@@ -198,9 +200,9 @@ fn main() -> Result<(), Box<dyn Error>> {
198200
let contents =
199201
format!("{use_deps}\n\n{use_statements}\n\n{action_kind_def}\n\n{action_kind_get_impls}\n");
200202

201-
fs::write(crate_dir.join("src/action_kind.rs"), contents)?;
202-
203-
std::process::Command::new("cargo").arg("fmt").output()?;
203+
let path = crate_dir.join("src/action_kind.rs");
204+
fs::write(&path, contents)?;
205+
rust_format::RustFmt::default().format_file(&path).expect("failed to format generated file");
204206

205207
Ok(())
206208
}

snarker/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ p2p = { path = "../p2p" }
2929

3030
[build-dependencies]
3131
regex = "1"
32+
rust-format = "0.3"

snarker/build.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::io::{self, BufRead, BufReader};
66
use std::path::PathBuf;
77

88
use regex::Regex;
9+
use rust_format::Formatter;
910

1011
fn visit_dirs(dir: &PathBuf, cb: &mut dyn FnMut(&DirEntry)) -> io::Result<()> {
1112
if dir.is_dir() {
@@ -72,7 +73,9 @@ fn main() -> Result<(), Box<dyn Error>> {
7273
let Some(line) = lines.next() else { break };
7374
let line = line.unwrap();
7475

75-
let Some(matches) = action_def_re.captures(&line) else { continue };
76+
let Some(matches) = action_def_re.captures(&line) else {
77+
continue;
78+
};
7679
match &matches[1] {
7780
"struct" => {
7881
if let Some(action_name) = matches.get(2) {
@@ -207,9 +210,9 @@ fn main() -> Result<(), Box<dyn Error>> {
207210
let contents =
208211
format!("{use_deps}\n\n{use_statements}\n\n{action_kind_def}\n\n{action_kind_get_impls}\n");
209212

210-
fs::write(crate_dir.join("src/action_kind.rs"), contents)?;
211-
212-
std::process::Command::new("cargo").arg("fmt").output()?;
213+
let path = crate_dir.join("src/action_kind.rs");
214+
fs::write(&path, contents)?;
215+
rust_format::RustFmt::default().format_file(&path).expect("failed to format generated file");
213216

214217
Ok(())
215218
}

0 commit comments

Comments
 (0)