Skip to content

Commit a388bb3

Browse files
author
liv
committed
split codebase
1 parent 679508b commit a388bb3

File tree

4 files changed

+170
-155
lines changed

4 files changed

+170
-155
lines changed

src/main.rs

Lines changed: 7 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
use clap::{App, Arg, SubCommand, crate_version};
2-
use console::{style, Emoji};
3-
use indicatif::ProgressBar;
42
use syntect::easy::HighlightFile;
53
use syntect::parsing::SyntaxSet;
64
use syntect::highlighting::{ThemeSet, Style};
75
use syntect::util::{as_24_bit_terminal_escaped};
8-
use std::fs::remove_file;
96
use std::io::BufRead;
10-
use std::process::Command;
117
use std::sync::mpsc::channel;
128
use std::time::Duration;
139
use notify::DebouncedEvent;
1410
use notify::{RecommendedWatcher, Watcher, RecursiveMode};
11+
use crate::verify::verify;
12+
use crate::run::run;
13+
14+
mod run;
15+
mod verify;
16+
mod util;
1517

1618
fn main() {
1719
let matches = App::new("rustlings")
@@ -38,48 +40,7 @@ fn main() {
3840
println!("");
3941

4042
if let Some(matches) = matches.subcommand_matches("run") {
41-
if let Some(filename) = matches.value_of("file") {
42-
let bar = ProgressBar::new_spinner();
43-
bar.set_message(format!("Compiling {}...", filename).as_str());
44-
bar.enable_steady_tick(100);
45-
let compilecmd = Command::new("rustc")
46-
.args(&[filename, "-o", "temp"])
47-
.output()
48-
.expect("fail");
49-
bar.set_message(format!("Running {}...", filename).as_str());
50-
if compilecmd.status.success() {
51-
let runcmd = Command::new("./temp").output().expect("fail");
52-
bar.finish_and_clear();
53-
54-
if runcmd.status.success() {
55-
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
56-
let formatstr =
57-
format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
58-
println!("{}", style(formatstr).green());
59-
clean();
60-
} else {
61-
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
62-
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
63-
64-
let formatstr =
65-
format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
66-
println!("{}", style(formatstr).red());
67-
clean();
68-
}
69-
} else {
70-
bar.finish_and_clear();
71-
let formatstr = format!(
72-
"{} Compilation of {} failed! Compiler error message:\n",
73-
Emoji("⚠️ ", "!"),
74-
filename
75-
);
76-
println!("{}", style(formatstr).red());
77-
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
78-
clean();
79-
}
80-
} else {
81-
panic!("Please supply a filename!");
82-
}
43+
run(matches.clone());
8344
}
8445

8546
if let Some(_) = matches.subcommand_matches("verify") {
@@ -128,112 +89,3 @@ fn watch() -> notify::Result<()> {
12889
}
12990
}
13091
}
131-
132-
fn verify() -> Result<(), ()> {
133-
compile_only("exercises/ex1.rs")?;
134-
compile_only("exercises/ex2.rs")?;
135-
compile_only("exercises/ex3.rs")?;
136-
compile_only("exercises/ex4.rs")?;
137-
compile_only("exercises/ex5.rs")?;
138-
compile_only("exercises/variables/variables1.rs")?;
139-
compile_only("exercises/variables/variables2.rs")?;
140-
compile_only("exercises/variables/variables3.rs")?;
141-
compile_only("exercises/variables/variables4.rs")?;
142-
compile_only("exercises/functions/functions1.rs")?;
143-
compile_only("exercises/functions/functions2.rs")?;
144-
compile_only("exercises/functions/functions3.rs")?;
145-
compile_only("exercises/functions/functions4.rs")?;
146-
compile_only("exercises/functions/functions5.rs")?;
147-
compile_only("exercises/primitive_types/primitive_types1.rs")?;
148-
compile_only("exercises/primitive_types/primitive_types2.rs")?;
149-
compile_only("exercises/primitive_types/primitive_types3.rs")?;
150-
compile_only("exercises/primitive_types/primitive_types4.rs")?;
151-
compile_only("exercises/primitive_types/primitive_types5.rs")?;
152-
compile_only("exercises/primitive_types/primitive_types6.rs")?;
153-
test("exercises/tests/tests1.rs")?;
154-
test("exercises/tests/tests2.rs")?;
155-
test("exercises/tests/tests3.rs")?;
156-
test("exercises/tests/tests4.rs")?;
157-
test("exercises/if/if1.rs")?;
158-
compile_only("exercises/strings/strings1.rs")?;
159-
compile_only("exercises/strings/strings2.rs")?;
160-
compile_only("exercises/strings/strings3.rs")?;
161-
compile_only("exercises/move_semantics/move_semantics1.rs")?;
162-
compile_only("exercises/move_semantics/move_semantics2.rs")?;
163-
compile_only("exercises/move_semantics/move_semantics3.rs")?;
164-
compile_only("exercises/move_semantics/move_semantics4.rs")?;
165-
compile_only("exercises/modules/modules1.rs")?;
166-
compile_only("exercises/modules/modules2.rs")?;
167-
compile_only("exercises/macros/macros1.rs")?;
168-
compile_only("exercises/macros/macros2.rs")?;
169-
compile_only("exercises/macros/macros3.rs")?;
170-
compile_only("exercises/macros/macros4.rs")?;
171-
test("exercises/error_handling/errors1.rs")?;
172-
test("exercises/error_handling/errors2.rs")?;
173-
test("exercises/error_handling/errors3.rs")?;
174-
test("exercises/error_handling/errorsn.rs")?;
175-
compile_only("exercises/error_handling/option1.rs")?;
176-
test("exercises/error_handling/result1.rs")?;
177-
Ok(())
178-
}
179-
180-
fn compile_only(filename: &str) -> Result<(), ()> {
181-
let bar = ProgressBar::new_spinner();
182-
bar.set_message(format!("Compiling {}...", filename).as_str());
183-
bar.enable_steady_tick(100);
184-
let compilecmd = Command::new("rustc")
185-
.args(&[filename, "-o", "temp", "--color", "always"])
186-
.output()
187-
.expect("fail");
188-
bar.finish_and_clear();
189-
if compilecmd.status.success() {
190-
let formatstr = format!(
191-
"{} Successfully compiled {}!",
192-
Emoji("✅", "✓"),
193-
filename
194-
);
195-
println!("{}", style(formatstr).green());
196-
clean();
197-
Ok(())
198-
} else {
199-
let formatstr = format!(
200-
"{} Compilation of {} failed! Compiler error message:\n",
201-
Emoji("⚠️ ", "!"),
202-
filename
203-
);
204-
println!("{}", style(formatstr).red());
205-
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
206-
clean();
207-
Err(())
208-
}
209-
}
210-
211-
fn test(filename: &str) -> Result<(), ()> {
212-
let bar = ProgressBar::new_spinner();
213-
bar.set_message(format!("Testing {}...", filename).as_str());
214-
bar.enable_steady_tick(100);
215-
let testcmd = Command::new("rustc")
216-
.args(&["--test", filename, "-o", "temp"])
217-
.output()
218-
.expect("fail");
219-
bar.finish_and_clear();
220-
if testcmd.status.success() {
221-
let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), filename);
222-
println!("{}", style(formatstr).green());
223-
clean();
224-
Ok(())
225-
} else {
226-
let formatstr = format!(
227-
"{} Testing of {} failed! Please try again.",
228-
Emoji("⚠️ ", "!"),
229-
filename
230-
);
231-
println!("{}", style(formatstr).red());
232-
clean();
233-
Err(())
234-
}
235-
}
236-
237-
fn clean() {
238-
let _ignored = remove_file("temp");
239-
}

src/run.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use console::{style, Emoji};
2+
use indicatif::ProgressBar;
3+
use std::process::Command;
4+
use crate::util::clean;
5+
6+
pub fn run(matches: clap::ArgMatches) {
7+
if let Some(filename) = matches.value_of("file") {
8+
let bar = ProgressBar::new_spinner();
9+
bar.set_message(format!("Compiling {}...", filename).as_str());
10+
bar.enable_steady_tick(100);
11+
let compilecmd = Command::new("rustc")
12+
.args(&[filename, "-o", "temp"])
13+
.output()
14+
.expect("fail");
15+
bar.set_message(format!("Running {}...", filename).as_str());
16+
if compilecmd.status.success() {
17+
let runcmd = Command::new("./temp").output().expect("fail");
18+
bar.finish_and_clear();
19+
20+
if runcmd.status.success() {
21+
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
22+
let formatstr =
23+
format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
24+
println!("{}", style(formatstr).green());
25+
clean();
26+
} else {
27+
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
28+
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
29+
30+
let formatstr =
31+
format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
32+
println!("{}", style(formatstr).red());
33+
clean();
34+
}
35+
} else {
36+
bar.finish_and_clear();
37+
let formatstr = format!(
38+
"{} Compilation of {} failed! Compiler error message:\n",
39+
Emoji("⚠️ ", "!"),
40+
filename
41+
);
42+
println!("{}", style(formatstr).red());
43+
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
44+
clean();
45+
}
46+
} else {
47+
panic!("Please supply a filename!");
48+
}
49+
}

src/util.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::fs::remove_file;
2+
3+
pub fn clean() {
4+
let _ignored = remove_file("temp");
5+
}

src/verify.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use console::{style, Emoji};
2+
use indicatif::ProgressBar;
3+
use std::process::Command;
4+
use crate::util::clean;
5+
6+
pub fn verify() -> Result<(), ()> {
7+
compile_only("exercises/ex1.rs")?;
8+
compile_only("exercises/ex2.rs")?;
9+
compile_only("exercises/ex3.rs")?;
10+
compile_only("exercises/ex4.rs")?;
11+
compile_only("exercises/ex5.rs")?;
12+
compile_only("exercises/variables/variables1.rs")?;
13+
compile_only("exercises/variables/variables2.rs")?;
14+
compile_only("exercises/variables/variables3.rs")?;
15+
compile_only("exercises/variables/variables4.rs")?;
16+
compile_only("exercises/functions/functions1.rs")?;
17+
compile_only("exercises/functions/functions2.rs")?;
18+
compile_only("exercises/functions/functions3.rs")?;
19+
compile_only("exercises/functions/functions4.rs")?;
20+
compile_only("exercises/functions/functions5.rs")?;
21+
compile_only("exercises/primitive_types/primitive_types1.rs")?;
22+
compile_only("exercises/primitive_types/primitive_types2.rs")?;
23+
compile_only("exercises/primitive_types/primitive_types3.rs")?;
24+
compile_only("exercises/primitive_types/primitive_types4.rs")?;
25+
compile_only("exercises/primitive_types/primitive_types5.rs")?;
26+
compile_only("exercises/primitive_types/primitive_types6.rs")?;
27+
test("exercises/tests/tests1.rs")?;
28+
test("exercises/tests/tests2.rs")?;
29+
test("exercises/tests/tests3.rs")?;
30+
test("exercises/tests/tests4.rs")?;
31+
test("exercises/if/if1.rs")?;
32+
compile_only("exercises/strings/strings1.rs")?;
33+
compile_only("exercises/strings/strings2.rs")?;
34+
compile_only("exercises/strings/strings3.rs")?;
35+
compile_only("exercises/move_semantics/move_semantics1.rs")?;
36+
compile_only("exercises/move_semantics/move_semantics2.rs")?;
37+
compile_only("exercises/move_semantics/move_semantics3.rs")?;
38+
compile_only("exercises/move_semantics/move_semantics4.rs")?;
39+
compile_only("exercises/modules/modules1.rs")?;
40+
compile_only("exercises/modules/modules2.rs")?;
41+
compile_only("exercises/macros/macros1.rs")?;
42+
compile_only("exercises/macros/macros2.rs")?;
43+
compile_only("exercises/macros/macros3.rs")?;
44+
compile_only("exercises/macros/macros4.rs")?;
45+
test("exercises/error_handling/errors1.rs")?;
46+
test("exercises/error_handling/errors2.rs")?;
47+
test("exercises/error_handling/errors3.rs")?;
48+
test("exercises/error_handling/errorsn.rs")?;
49+
compile_only("exercises/error_handling/option1.rs")?;
50+
test("exercises/error_handling/result1.rs")?;
51+
Ok(())
52+
}
53+
54+
fn compile_only(filename: &str) -> Result<(), ()> {
55+
let bar = ProgressBar::new_spinner();
56+
bar.set_message(format!("Compiling {}...", filename).as_str());
57+
bar.enable_steady_tick(100);
58+
let compilecmd = Command::new("rustc")
59+
.args(&[filename, "-o", "temp", "--color", "always"])
60+
.output()
61+
.expect("fail");
62+
bar.finish_and_clear();
63+
if compilecmd.status.success() {
64+
let formatstr = format!(
65+
"{} Successfully compiled {}!",
66+
Emoji("✅", "✓"),
67+
filename
68+
);
69+
println!("{}", style(formatstr).green());
70+
clean();
71+
Ok(())
72+
} else {
73+
let formatstr = format!(
74+
"{} Compilation of {} failed! Compiler error message:\n",
75+
Emoji("⚠️ ", "!"),
76+
filename
77+
);
78+
println!("{}", style(formatstr).red());
79+
println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
80+
clean();
81+
Err(())
82+
}
83+
}
84+
85+
fn test(filename: &str) -> Result<(), ()> {
86+
let bar = ProgressBar::new_spinner();
87+
bar.set_message(format!("Testing {}...", filename).as_str());
88+
bar.enable_steady_tick(100);
89+
let testcmd = Command::new("rustc")
90+
.args(&["--test", filename, "-o", "temp"])
91+
.output()
92+
.expect("fail");
93+
bar.finish_and_clear();
94+
if testcmd.status.success() {
95+
let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), filename);
96+
println!("{}", style(formatstr).green());
97+
clean();
98+
Ok(())
99+
} else {
100+
let formatstr = format!(
101+
"{} Testing of {} failed! Please try again.",
102+
Emoji("⚠️ ", "!"),
103+
filename
104+
);
105+
println!("{}", style(formatstr).red());
106+
clean();
107+
Err(())
108+
}
109+
}

0 commit comments

Comments
 (0)