Skip to content

Commit 7676a62

Browse files
Merge #349
349: Refactoring. r=therealprof a=reitermarkus Co-authored-by: Markus Reiter <[email protected]>
2 parents 4535b6b + 2758bb5 commit 7676a62

File tree

9 files changed

+60
-64
lines changed

9 files changed

+60
-64
lines changed

Cargo.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ keywords = ["cross", "compilation", "testing", "tool"]
77
license = "MIT OR Apache-2.0"
88
name = "cross"
99
repository = "https://github.com/rust-embedded/cross"
10-
version = "0.1.16"
10+
version = "0.2.0-alpha.1"
1111
edition = "2018"
1212

1313
[dependencies]

src/cargo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ pub enum Subcommand {
1919
}
2020

2121
impl Subcommand {
22-
pub fn needs_docker(&self) -> bool {
23-
match *self {
22+
pub fn needs_docker(self) -> bool {
23+
match self {
2424
Subcommand::Other => false,
2525
_ => true,
2626
}
2727
}
2828

29-
pub fn needs_interpreter(&self) -> bool {
30-
match *self {
29+
pub fn needs_interpreter(self) -> bool {
30+
match self {
3131
Subcommand::Run | Subcommand::Test | Subcommand::Bench => true,
3232
_ => false,
3333
}

src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub fn parse(target_list: &TargetList) -> Args {
3636
}
3737

3838
Args {
39-
all: all,
39+
all,
4040
subcommand: sc,
41-
target: target,
41+
target,
4242
}
4343
}

src/docker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ pub fn run(target: &Target,
134134
}
135135

136136
if let Ok(value) = env::var("DOCKER_OPTS") {
137-
let opts: Vec<&str> = value.split(" ").collect();
137+
let opts: Vec<&str> = value.split(' ').collect();
138138
docker.args(&opts);
139139
}
140140

141141
docker
142-
.args(&["-e", &format!("CROSS_RUNNER={}", runner.unwrap_or_else(|| String::new()))])
142+
.args(&["-e", &format!("CROSS_RUNNER={}", runner.unwrap_or_else(String::new))])
143143
.args(&["-v", &format!("{}:/xargo:Z", xargo_dir.display())])
144144
.args(&["-v", &format!("{}:/cargo:Z", cargo_dir.display())])
145145
// Prevent `bin` from being mounted inside the Docker container.

src/extensions.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,50 @@ use std::process::{Command, ExitStatus};
33
use crate::errors::*;
44

55
pub trait CommandExt {
6+
fn print_verbose(&self, verbose: bool);
7+
fn status_result(&self, status: ExitStatus) -> Result<()>;
68
fn run(&mut self, verbose: bool) -> Result<()>;
79
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus>;
810
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String>;
911
}
1012

1113
impl CommandExt for Command {
12-
/// Runs the command to completion
13-
fn run(&mut self, verbose: bool) -> Result<()> {
14-
let status = self.run_and_get_status(verbose)?;
14+
fn print_verbose(&self, verbose: bool) {
15+
if verbose {
16+
println!("+ {:?}", self);
17+
}
18+
}
1519

20+
fn status_result(&self, status: ExitStatus) -> Result<()> {
1621
if status.success() {
1722
Ok(())
1823
} else {
19-
Err(format!("`{:?}` failed with exit code: {:?}",
20-
self,
21-
status.code()))?
24+
Err(format!("`{:?}` failed with exit code: {:?}", self, status.code()).into())
2225
}
2326
}
2427

2528
/// Runs the command to completion
26-
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus> {
27-
if verbose {
28-
println!("+ {:?}", self);
29-
}
29+
fn run(&mut self, verbose: bool) -> Result<()> {
30+
let status = self.run_and_get_status(verbose)?;
31+
self.status_result(status)
32+
}
3033

34+
/// Runs the command to completion
35+
fn run_and_get_status(&mut self, verbose: bool) -> Result<ExitStatus> {
36+
self.print_verbose(verbose);
3137
self.status()
3238
.chain_err(|| format!("couldn't execute `{:?}`", self))
3339
}
3440

3541
/// Runs the command to completion and returns its stdout
3642
fn run_and_get_stdout(&mut self, verbose: bool) -> Result<String> {
37-
if verbose {
38-
println!("+ {:?}", self);
39-
}
40-
43+
self.print_verbose(verbose);
4144
let out = self.output()
4245
.chain_err(|| format!("couldn't execute `{:?}`", self))?;
4346

44-
if out.status.success() {
45-
Ok(String::from_utf8(out.stdout).chain_err(|| {
46-
format!("`{:?}` output was not UTF-8",
47-
self)
48-
})?)
49-
} else {
50-
Err(format!("`{:?}` failed with exit code: {:?}",
51-
self,
52-
out.status.code()))?
53-
}
47+
self.status_result(out.status)?;
48+
49+
Ok(String::from_utf8(out.stdout)
50+
.chain_err(|| format!("`{:?}` output was not UTF-8", self))?)
5451
}
5552
}

src/interpreter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::Target;
77
/// Checks if the interpreters have been registered in the host system
88
pub fn is_registered(target: &Target) -> Result<bool> {
99
if file::read("/proc/sys/fs/binfmt_misc/status")?.trim() != "enabled" {
10-
Err("host system doesn't have binfmt_misc support")?
10+
return Err("host system doesn't have binfmt_misc support".into());
1111
}
1212

1313
let ok = if target.is_windows() {

src/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn run() -> Result<ExitStatus> {
225225

226226
if host.is_supported(args.target.as_ref()) {
227227
let target = args.target
228-
.unwrap_or(Target::from(host.triple(), &target_list));
228+
.unwrap_or_else(|| Target::from(host.triple(), &target_list));
229229
let toml = toml(&root)?;
230230

231231
let sysroot = rustc::sysroot(&host, &target, verbose)?;
@@ -254,10 +254,9 @@ fn run() -> Result<ExitStatus> {
254254
rustup::install_component("rust-src", toolchain, verbose)?;
255255
}
256256

257-
if args.subcommand.map(|sc| sc == Subcommand::Clippy).unwrap_or(false) {
258-
if !rustup::component_is_installed("clippy", toolchain, verbose)? {
259-
rustup::install_component("clippy", toolchain, verbose)?;
260-
}
257+
if args.subcommand.map(|sc| sc == Subcommand::Clippy).unwrap_or(false) &&
258+
!rustup::component_is_installed("clippy", toolchain, verbose)? {
259+
rustup::install_component("clippy", toolchain, verbose)?;
261260
}
262261

263262
let needs_interpreter = args.subcommand.map(|sc| sc.needs_interpreter()).unwrap_or(false);

src/rustc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ impl VersionMetaExt for VersionMeta {
2828
}
2929

3030
fn needs_interpreter(&self) -> bool {
31-
!(self.semver >= Version {
31+
self.semver < Version {
3232
major: 1,
3333
minor: 19,
3434
patch: 0,
3535
pre: vec![],
3636
build: vec![],
37-
})
37+
}
3838
}
3939
}
4040

0 commit comments

Comments
 (0)