From 4b7899e8d452966a63bff5388423711a29925752 Mon Sep 17 00:00:00 2001 From: blek Date: Wed, 1 Nov 2023 01:28:00 +1000 Subject: [PATCH] get rid of function panics in the build.rs --- filed/build.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/filed/build.rs b/filed/build.rs index 04b9494..a3b0f11 100644 --- a/filed/build.rs +++ b/filed/build.rs @@ -1,5 +1,5 @@ -use std::{fs, path::PathBuf, ffi::OsStr, process::Command}; +use std::{fs, path::PathBuf, ffi::OsStr, process::Command, error::Error}; use css_minify::optimizations::{Minifier, Level}; @@ -17,17 +17,17 @@ fn extfilter(valid: String, x: Option<&OsStr>) -> bool { ext == valid } -fn system(cmd: &str, args: &[&str]) -> String { +fn system(cmd: &str, args: &[&str]) -> Result> { let out = Command::new(cmd) .args(args) .output() - .unwrap(); + ?; if out.stderr.len() != 0 { panic!("Got this while running {cmd} with \"{}\": {}", args.join(" "), String::from_utf8(out.stderr).unwrap()) } - String::from_utf8(out.stdout).unwrap() + Ok(String::from_utf8(out.stdout)?) } fn main() { @@ -65,17 +65,24 @@ fn main() { scripts.iter().for_each(|asset| { Command::new("uglifyjs") - .arg("-c") .arg(asset) .arg("-o") .arg(asset_path(asset)) + .arg("-c") .spawn() .unwrap(); }); - let commit = system("git", &["rev-parse", "HEAD"]); - let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]); + let commit = system("git", &["rev-parse", "HEAD"]).map_err(|x| x.to_string()); + let branch = system("git", &["rev-parse", "--abbrev-ref", "HEAD"]).map_err(|x| x.to_string()); - println!("cargo:rustc-env=COMMIT_HASH={commit}"); - println!("cargo:rustc-env=COMMIT_BRANCH={branch}"); + match commit { + Err(err) => panic!("Can't get commit: {}", err), + Ok(commit) => println!("cargo:rustc-env=COMMIT_HASH={commit}") + } + + match branch { + Err(err) => panic!("Can't get commit: {}", err), + Ok(branch) => println!("cargo:rustc-env=COMMIT_BRANCH={branch}") + } } \ No newline at end of file