From 28d947fd8be77946d9e281ec0c794a1c2df929de Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Tue, 31 Oct 2023 13:45:40 +0800 Subject: [PATCH] Use anyhow in build script --- Cargo.lock | 7 +++++++ Cargo.toml | 3 +++ build.rs | 13 +++++++------ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8bd4f6c9..70370f84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "anyhow" +version = "1.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" + [[package]] name = "assert_cmd" version = "2.0.12" @@ -106,6 +112,7 @@ name = "bat" version = "0.24.0" dependencies = [ "ansi_colours", + "anyhow", "assert_cmd", "bincode", "bugreport", diff --git a/Cargo.toml b/Cargo.toml index 4d6a5acf..d8e7ffe5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,6 +98,9 @@ tempfile = "3.8.1" [target.'cfg(unix)'.dev-dependencies] nix = { version = "0.26.4", default-features = false, features = ["term"] } +[build-dependencies] +anyhow = "1.0.75" + [build-dependencies.clap] version = "4.4.6" optional = true diff --git a/build.rs b/build.rs index cf0375bf..43543904 100644 --- a/build.rs +++ b/build.rs @@ -4,9 +4,8 @@ fn main() {} #[cfg(feature = "application")] -fn main() -> Result<(), Box> { +fn main() -> anyhow::Result<()> { use std::collections::HashMap; - use std::error::Error; use std::fs; use std::path::Path; @@ -21,7 +20,7 @@ fn main() -> Result<(), Box> { variables: &HashMap<&str, &str>, in_file: &str, out_file: impl AsRef, - ) -> Result<(), Box> { + ) -> anyhow::Result<()> { let mut content = fs::read_to_string(in_file)?; for (variable_name, value) in variables { @@ -40,9 +39,11 @@ fn main() -> Result<(), Box> { variables.insert("PROJECT_EXECUTABLE_UPPERCASE", &executable_name_uppercase); variables.insert("PROJECT_VERSION", PROJECT_VERSION); - let out_dir_env = std::env::var_os("BAT_ASSETS_GEN_DIR") - .or_else(|| std::env::var_os("OUT_DIR")) - .expect("BAT_ASSETS_GEN_DIR or OUT_DIR to be set in build.rs"); + let Some(out_dir_env) = + std::env::var_os("BAT_ASSETS_GEN_DIR").or_else(|| std::env::var_os("OUT_DIR")) + else { + anyhow::bail!("BAT_ASSETS_GEN_DIR or OUT_DIR should be set for build.rs"); + }; let out_dir = Path::new(&out_dir_env); fs::create_dir_all(out_dir.join("assets/manual")).unwrap();