Use Result.expect() in the suitable cases

This commit is contained in:
Nathan Fiedler
2016-07-14 19:12:27 -07:00
parent dff74fac98
commit 96071ca377

View File

@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path; use std::path::Path;
@ -38,7 +37,7 @@ fn main() {
.arg("clone") .arg("clone")
.arg("https://github.com/crabtw/rust-bindgen.git") .arg("https://github.com/crabtw/rust-bindgen.git")
.arg(bindgen_path) .arg(bindgen_path)
.status().unwrap(); .status().expect("git clone rust-bindgen");
// Checkout a version of rust-bindgen that is known to work; // Checkout a version of rust-bindgen that is known to work;
// more recent versions produce code that does not compile (the // more recent versions produce code that does not compile (the
// commit after 8a51860 changes the way enums are generated). // commit after 8a51860 changes the way enums are generated).
@ -46,7 +45,7 @@ fn main() {
.arg("checkout") .arg("checkout")
.arg("8a51860") .arg("8a51860")
.current_dir(bindgen_path) .current_dir(bindgen_path)
.status().unwrap(); .status().expect("git checkout");
} }
let mut bindgen_bin = bindgen_path.to_path_buf(); let mut bindgen_bin = bindgen_path.to_path_buf();
@ -55,34 +54,28 @@ fn main() {
let mut cmd = Command::new("cargo"); let mut cmd = Command::new("cargo");
cmd.arg("build").current_dir(bindgen_path); cmd.arg("build").current_dir(bindgen_path);
println!("BINDGEN_BUILD={:?}", cmd); println!("BINDGEN_BUILD={:?}", cmd);
cmd.status().unwrap(); cmd.status().expect("cargo build");
} }
// Get the compiler and linker flags for the MagickWand library. // Get the compiler and linker flags for the MagickWand library.
let mw_cflags_output = Command::new("pkg-config") let mw_cflags_output = Command::new("pkg-config")
.arg("--cflags") .arg("--cflags")
.arg("MagickWand") .arg("MagickWand")
.output().unwrap(); .output().expect("pkg-config --cflags MagickWand");
let mw_cflags = std::str::from_utf8(&mw_cflags_output.stdout).unwrap().trim(); let mw_cflags = std::str::from_utf8(&mw_cflags_output.stdout).unwrap().trim();
let mw_cflags_arr: Vec<&str> = mw_cflags.split_whitespace().collect(); let mw_cflags_arr: Vec<&str> = mw_cflags.split_whitespace().collect();
println!("CFLAGS={:?}", mw_cflags_arr); println!("CFLAGS={:?}", mw_cflags_arr);
let mw_ldflags_output = Command::new("pkg-config") let mw_ldflags_output = Command::new("pkg-config")
.arg("--libs") .arg("--libs")
.arg("MagickWand") .arg("MagickWand")
.output().unwrap(); .output().expect("pkg-config --libs MagickWand");
let mw_ldflags = std::str::from_utf8(&mw_ldflags_output.stdout).unwrap().trim(); let mw_ldflags = std::str::from_utf8(&mw_ldflags_output.stdout).unwrap().trim();
let mw_ldflags_arr: Vec<&str> = mw_ldflags.split_whitespace().collect(); let mw_ldflags_arr: Vec<&str> = mw_ldflags.split_whitespace().collect();
println!("LDFLAGS={:?}", mw_ldflags_arr); println!("LDFLAGS={:?}", mw_ldflags_arr);
let gen_h_path = out_dir.clone() + "/gen.h"; let gen_h_path = out_dir.clone() + "/gen.h";
// Create the header file that rust-bindgen needs as input. // Create the header file that rust-bindgen needs as input.
let mut gen_h = match File::create(&gen_h_path) { let mut gen_h = File::create(&gen_h_path).expect("could not create file");
Err(why) => panic!("could not create {} file: {}", gen_h_path, Error::description(&why)), gen_h.write_all(HEADER.as_bytes()).expect("could not write header file");
Ok(file) => file
};
match gen_h.write_all(HEADER.as_bytes()) {
Err(why) => panic!("could not write to {}: {}", gen_h_path, Error::description(&why)),
Ok(_) => ()
};
// Combine all of that in the invocation of rust-bindgen. // Combine all of that in the invocation of rust-bindgen.
let mut cmd = &mut Command::new(bindgen_bin); let mut cmd = &mut Command::new(bindgen_bin);
@ -108,16 +101,13 @@ fn main() {
.args(&mw_ldflags_arr[..]) .args(&mw_ldflags_arr[..])
.arg(&gen_h_path); .arg(&gen_h_path);
println!("BINDING_GENERATION={:?}", cmd); println!("BINDING_GENERATION={:?}", cmd);
cmd.status().unwrap(); cmd.status().expect("rust-bindgen invocation");
// how to get the output of the command... // how to get the output of the command...
// let output = Commad::new(...).output().unwrap(); // let output = Commad::new(...).output().unwrap();
// let out = std::str::from_utf8(&output.stdout).unwrap(); // let out = std::str::from_utf8(&output.stdout).unwrap();
// println!("cargo:output={}", out); // println!("cargo:output={}", out);
// let err = std::str::from_utf8(&output.stderr).unwrap(); // let err = std::str::from_utf8(&output.stderr).unwrap();
// println!("cargo:error={}", err); // println!("cargo:error={}", err);
match std::fs::remove_file(&gen_h_path) { std::fs::remove_file(&gen_h_path).expect("could not remove header file");
Err(why) => panic!("could not remove {}: {}", gen_h_path, Error::description(&why)),
Ok(_) => ()
}
} }
} }