From eb8ba0941da2d9e6451740beb4091d47b7238121 Mon Sep 17 00:00:00 2001 From: Jo <10510431+j178@users.noreply.github.com> Date: Sat, 2 Aug 2025 12:36:58 +0800 Subject: [PATCH] Use `anyhow::Context` (#303) --- src/builtin/pre_commit_hooks/check_added_large_files.rs | 3 ++- src/builtin/pre_commit_hooks/fix_trailing_whitespace.rs | 4 ++-- src/languages/docker.rs | 2 +- src/languages/node/node.rs | 5 ++--- src/languages/python/python.rs | 3 +-- src/languages/script.rs | 4 +++- src/languages/system.rs | 4 +++- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/builtin/pre_commit_hooks/check_added_large_files.rs b/src/builtin/pre_commit_hooks/check_added_large_files.rs index 89d8288f..3c7e8e6e 100644 --- a/src/builtin/pre_commit_hooks/check_added_large_files.rs +++ b/src/builtin/pre_commit_hooks/check_added_large_files.rs @@ -1,5 +1,6 @@ use std::collections::{HashMap, HashSet}; +use anyhow::Context; use clap::Parser; use futures::StreamExt; @@ -34,7 +35,7 @@ pub(crate) async fn check_added_large_files( filenames: &[&String], _env_vars: &HashMap<&'static str, String>, ) -> anyhow::Result<(i32, Vec)> { - let entry = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?; + let entry = shlex::split(&hook.entry).context("Failed to parse entry")?; let args = Args::try_parse_from(entry.iter().chain(&hook.args))?; let filter = if args.enforce_all { diff --git a/src/builtin/pre_commit_hooks/fix_trailing_whitespace.rs b/src/builtin/pre_commit_hooks/fix_trailing_whitespace.rs index 6bad779e..d31d6ec6 100644 --- a/src/builtin/pre_commit_hooks/fix_trailing_whitespace.rs +++ b/src/builtin/pre_commit_hooks/fix_trailing_whitespace.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::path::Path; -use anyhow::Result; +use anyhow::{Context, Result}; use bstr::ByteSlice; use clap::Parser; use futures::StreamExt; @@ -22,7 +22,7 @@ pub(crate) async fn fix_trailing_whitespace( filenames: &[&String], _env_vars: &HashMap<&'static str, String>, ) -> Result<(i32, Vec)> { - let entry = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?; + let entry = shlex::split(&hook.entry).context("Failed to parse entry")?; let args = Args::try_parse_from(entry.iter().chain(&hook.args))?; let force_markdown = args.markdown_linebreak_ext.iter().any(|ext| ext == "*"); diff --git a/src/languages/docker.rs b/src/languages/docker.rs index 06a0a5be..fd3df319 100644 --- a/src/languages/docker.rs +++ b/src/languages/docker.rs @@ -204,7 +204,7 @@ impl LanguageImpl for Docker { let docker_tag = Docker::docker_tag(hook); - let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?; + let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?; let run = async move |batch: Vec| { // docker run [OPTIONS] IMAGE [COMMAND] [ARG...] diff --git a/src/languages/node/node.rs b/src/languages/node/node.rs index e898c613..0b4bdc41 100644 --- a/src/languages/node/node.rs +++ b/src/languages/node/node.rs @@ -96,10 +96,9 @@ impl LanguageImpl for Node { env_vars: &HashMap<&'static str, String>, _store: &Store, ) -> Result<(i32, Vec), Error> { - let env_dir = hook.env_path().expect("Python must have env path"); + let env_dir = hook.env_path().expect("Node must have env path"); // TODO: move split to hook construction - let cmds = shlex::split(&hook.entry) - .ok_or_else(|| anyhow::anyhow!("Failed to parse entry command"))?; + let cmds = shlex::split(&hook.entry).context("Failed to parse entry command")?; let new_path = prepend_path(&bin_dir(env_dir)).context("Failed to join PATH")?; diff --git a/src/languages/python/python.rs b/src/languages/python/python.rs index 1c574f92..7147088e 100644 --- a/src/languages/python/python.rs +++ b/src/languages/python/python.rs @@ -130,8 +130,7 @@ impl LanguageImpl for Python { _store: &Store, ) -> Result<(i32, Vec), Error> { let env_dir = hook.env_path().expect("Python must have env path"); - let cmds = shlex::split(&hook.entry) - .ok_or_else(|| anyhow::anyhow!("Failed to parse entry command"))?; + let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?; // Construct PATH with venv bin directory first let new_path = prepend_path(&bin_dir(env_dir)).context("Failed to join PATH")?; diff --git a/src/languages/script.rs b/src/languages/script.rs index 55b7701e..72629cf4 100644 --- a/src/languages/script.rs +++ b/src/languages/script.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use anyhow::Context; + use crate::hook::Hook; use crate::hook::InstalledHook; use crate::languages::{Error, LanguageImpl}; @@ -26,7 +28,7 @@ impl LanguageImpl for Script { env_vars: &HashMap<&'static str, String>, _store: &Store, ) -> Result<(i32, Vec), Error> { - let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?; + let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?; let run = async move |batch: Vec| { let mut command = Cmd::new(&cmds[0], "run script command") diff --git a/src/languages/system.rs b/src/languages/system.rs index bf430cfc..d30d9015 100644 --- a/src/languages/system.rs +++ b/src/languages/system.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use anyhow::Context; + use crate::hook::{Hook, InstalledHook}; use crate::languages::{Error, LanguageImpl}; use crate::process::Cmd; @@ -25,7 +27,7 @@ impl LanguageImpl for System { env_vars: &HashMap<&'static str, String>, _store: &Store, ) -> Result<(i32, Vec), Error> { - let cmds = shlex::split(&hook.entry).ok_or(anyhow::anyhow!("Failed to parse entry"))?; + let cmds = shlex::split(&hook.entry).context("Failed to parse entry")?; let run = async move |batch: Vec| { let mut output = Cmd::new(&cmds[0], "run system command")