Skip to content

Commit aa7fe99

Browse files
committed
Log entry 2: first implementation
I think I found the correct place in the algorithm where rustfmt should be called. I'm not sure if using `std::process::Command` and converting errors into warnings is the correct way here. Also the warning should only be printed once. Unless `config.shell().warn()` deduplicates warnings, right now there will be one warning per source file. So I should probably check that `rustfmt` exists only once and disable formatting if it doesn't.
1 parent 6057c93 commit aa7fe99

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fmt;
1212
use std::fs;
1313
use std::io::{BufRead, BufReader, ErrorKind};
1414
use std::path::{Path, PathBuf};
15+
use std::process::Command;
1516
use std::str::FromStr;
1617

1718
use toml;
@@ -573,8 +574,6 @@ fn init_vcs(path: &Path, vcs: VersionControl, config: &Config) -> CargoResult<()
573574
}
574575

575576
fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
576-
println!("Entering 'mk'");
577-
578577
let path = opts.path;
579578
let name = opts.name;
580579
let cfg = config.get::<CargoNewConfig>("cargo-new")?;
@@ -709,6 +708,12 @@ mod tests {
709708
.unwrap_or(false)
710709
{
711710
paths::write(&path_of_source_file, default_file_content)?;
711+
712+
// Format the newly created source file with rustfmt
713+
if let Err(e) = Command::new("rustfmt").arg(&path_of_source_file).output() {
714+
let msg = format!("failed to format {}: {}", path_of_source_file.display(), e);
715+
config.shell().warn(msg)?;
716+
}
712717
}
713718
}
714719

@@ -721,8 +726,6 @@ mod tests {
721726
config.shell().warn(msg)?;
722727
}
723728

724-
println!("Leaving 'mk'");
725-
726729
Ok(())
727730
}
728731

0 commit comments

Comments
 (0)