Factor creating a GlobSet from a Vec<String> out to a utility function.

This commit is contained in:
Bauke 2022-11-28 22:48:35 +01:00
parent 47b1b7ec51
commit 0d09e2e086
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 15 additions and 10 deletions

View File

@ -4,14 +4,13 @@ use std::{io::Read, process::exit};
use { use {
color_eyre::{eyre::eyre, Result}, color_eyre::{eyre::eyre, Result},
globset::{Glob, GlobSetBuilder},
hooked_config::{Config, ExitAction}, hooked_config::{Config, ExitAction},
owo_colors::{OwoColorize, Style}, owo_colors::{OwoColorize, Style},
subprocess::{Exec, Redirection}, subprocess::{Exec, Redirection},
supports_color::Stream, supports_color::Stream,
}; };
use crate::utilities::plural; use crate::utilities::{globset_from_strings, plural};
/// The `run` subcommand. /// The `run` subcommand.
pub fn hooked_run(config: Config, hook_type: String) -> Result<()> { pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
@ -40,14 +39,7 @@ pub fn hooked_run(config: Config, hook_type: String) -> Result<()> {
let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string()); let hook_name = hook.name.unwrap_or_else(|| "Unnamed Hook".to_string());
if !hook.staged.is_empty() { if !hook.staged.is_empty() {
let globs = { let globs = globset_from_strings(&hook.staged)?;
let mut builder = GlobSetBuilder::new();
for glob in hook.staged {
builder.add(Glob::new(&glob)?);
}
builder.build()?
};
let staged_files = Exec::cmd("git") let staged_files = Exec::cmd("git")
.args(&["diff", "--name-only", "--cached"]) .args(&["diff", "--name-only", "--cached"])

View File

@ -1,5 +1,8 @@
//! Miscellaneous utilities. //! Miscellaneous utilities.
use color_eyre::Result;
use globset::{Glob, GlobSet, GlobSetBuilder};
/// Simple function to create a pluralized string. /// Simple function to create a pluralized string.
pub fn plural(count: usize, singular: &str, plural: Option<&str>) -> String { pub fn plural(count: usize, singular: &str, plural: Option<&str>) -> String {
if count == 1 { if count == 1 {
@ -12,3 +15,13 @@ pub fn plural(count: usize, singular: &str, plural: Option<&str>) -> String {
format!("{singular}s") format!("{singular}s")
} }
/// Create a [`GlobSet`] from a list of strings.
pub fn globset_from_strings(input: &[String]) -> Result<GlobSet> {
let mut builder = GlobSetBuilder::new();
for glob in input {
builder.add(Glob::new(&glob)?);
}
builder.build().map_err(Into::into)
}