Skip to content

Commit 4fa79ee

Browse files
committed
Extract command builders into util
1 parent fbd0ccb commit 4fa79ee

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

src/run.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use crate::util::clean;
1+
use crate::util;
22
use crate::verify::test;
33
use console::{style, Emoji};
44
use indicatif::ProgressBar;
55
use std::fs;
6-
use std::process::Command;
76
use toml::Value;
87

98
pub fn run(matches: clap::ArgMatches) -> Result<(), ()> {
@@ -33,28 +32,26 @@ pub fn compile_and_run(filename: &str) -> Result<(), ()> {
3332
let progress_bar = ProgressBar::new_spinner();
3433
progress_bar.set_message(format!("Compiling {}...", filename).as_str());
3534
progress_bar.enable_steady_tick(100);
36-
let compilecmd = Command::new("rustc")
37-
.args(&[filename, "-o", "temp", "--color", "always"])
38-
.output()
39-
.expect("fail");
35+
36+
let compilecmd = util::compile_cmd(filename);
4037
progress_bar.set_message(format!("Running {}...", filename).as_str());
4138
if compilecmd.status.success() {
42-
let runcmd = Command::new("./temp").output().expect("fail");
39+
let runcmd = util::run_cmd();
4340
progress_bar.finish_and_clear();
4441

4542
if runcmd.status.success() {
4643
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
4744
let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
4845
println!("{}", style(formatstr).green());
49-
clean();
46+
util::clean();
5047
Ok(())
5148
} else {
5249
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
5350
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
5451

5552
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
5653
println!("{}", style(formatstr).red());
57-
clean();
54+
util::clean();
5855
Err(())
5956
}
6057
} else {
@@ -66,7 +63,7 @@ pub fn compile_and_run(filename: &str) -> Result<(), ()> {
6663
);
6764
println!("{}", style(formatstr).red());
6865
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
69-
clean();
66+
util::clean();
7067
Err(())
7168
}
7269
}

src/util.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
use std::fs::remove_file;
2+
use std::process::{Command, Output};
3+
4+
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
5+
6+
pub fn compile_test_cmd(filename: &str) -> Output {
7+
Command::new("rustc")
8+
.args(&["--test", filename, "-o", "temp"])
9+
.args(RUSTC_COLOR_ARGS)
10+
.output()
11+
.expect("failed to compile exercise")
12+
}
13+
14+
pub fn compile_cmd(filename: &str) -> Output {
15+
Command::new("rustc")
16+
.args(&[filename, "-o", "temp"])
17+
.args(RUSTC_COLOR_ARGS)
18+
.output()
19+
.expect("failed to compile exercise")
20+
}
21+
22+
pub fn run_cmd() -> Output {
23+
Command::new("./temp")
24+
.output()
25+
.expect("failed to run exercise")
26+
}
227

328
pub fn clean() {
429
let _ignored = remove_file("temp");

src/verify.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use crate::util::clean;
1+
use crate::util;
22
use console::{style, Emoji};
33
use indicatif::ProgressBar;
44
use std::fs;
5-
use std::process::Command;
65
use toml::Value;
76

87
pub fn verify(start_at: Option<&str>) -> Result<(), ()> {
@@ -34,10 +33,7 @@ fn compile_only(filename: &str) -> Result<(), ()> {
3433
let progress_bar = ProgressBar::new_spinner();
3534
progress_bar.set_message(format!("Compiling {}...", filename).as_str());
3635
progress_bar.enable_steady_tick(100);
37-
let compilecmd = Command::new("rustc")
38-
.args(&[filename, "-o", "temp", "--color", "always"])
39-
.output()
40-
.expect("fail");
36+
let compilecmd = util::compile_cmd(filename);
4137
progress_bar.finish_and_clear();
4238
if compilecmd.status.success() {
4339
let formatstr = format!(
@@ -46,7 +42,7 @@ fn compile_only(filename: &str) -> Result<(), ()> {
4642
filename
4743
);
4844
println!("{}", style(formatstr).green());
49-
clean();
45+
util::clean();
5046
Ok(())
5147
} else {
5248
let formatstr = format!(
@@ -56,7 +52,7 @@ fn compile_only(filename: &str) -> Result<(), ()> {
5652
);
5753
println!("{}", style(formatstr).red());
5854
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
59-
clean();
55+
util::clean();
6056
Err(())
6157
}
6258
}
@@ -65,19 +61,16 @@ pub fn test(filename: &str) -> Result<(), ()> {
6561
let progress_bar = ProgressBar::new_spinner();
6662
progress_bar.set_message(format!("Testing {}...", filename).as_str());
6763
progress_bar.enable_steady_tick(100);
68-
let testcmd = Command::new("rustc")
69-
.args(&["--test", filename, "-o", "temp", "--color", "always"])
70-
.output()
71-
.expect("fail");
64+
let testcmd = util::compile_test_cmd(filename);
7265
if testcmd.status.success() {
7366
progress_bar.set_message(format!("Running {}...", filename).as_str());
74-
let runcmd = Command::new("./temp").output().expect("fail");
67+
let runcmd = util::run_cmd();
7568
progress_bar.finish_and_clear();
7669

7770
if runcmd.status.success() {
7871
let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), filename);
7972
println!("{}", style(formatstr).green());
80-
clean();
73+
util::clean();
8174
Ok(())
8275
} else {
8376
let formatstr = format!(
@@ -87,7 +80,7 @@ pub fn test(filename: &str) -> Result<(), ()> {
8780
);
8881
println!("{}", style(formatstr).red());
8982
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
90-
clean();
83+
util::clean();
9184
Err(())
9285
}
9386
} else {
@@ -99,7 +92,7 @@ pub fn test(filename: &str) -> Result<(), ()> {
9992
);
10093
println!("{}", style(formatstr).red());
10194
println!("{}", String::from_utf8_lossy(&testcmd.stderr));
102-
clean();
95+
util::clean();
10396
Err(())
10497
}
10598
}

0 commit comments

Comments
 (0)