Skip to content
Myr

job.rs

This file

Path

src/exec/job.rs

63 lines · 2.1 KiB · Rust

Last changed by

fix: Better sanitizingThayne McCombsbefore adoption24198d9

History of this file

Take it

1use crate::config::Config;2use crate::exit_codes::{ExitCode, merge_exitcodes};3use crate::walk::WorkerResult;45use super::CommandSet;67/// An event loop that listens for inputs from the `rx` receiver. Each received input will8/// generate a command with the supplied command template. The generated command will then9/// be executed, and this process will continue until the receiver's sender has closed.10pub fn job(11    results: impl IntoIterator<Item = WorkerResult>,12    cmd: &CommandSet,13    config: &Config,14) -> ExitCode {15    // Output should be buffered when only running a single thread16    let buffer_output: bool = config.threads > 1;1718    let mut ret = ExitCode::Success;19    for result in results {20        // Obtain the next result from the receiver, else if the channel21        // has closed, exit from the loop22        let dir_entry = match result {23            WorkerResult::Entry(dir_entry) => dir_entry,24            WorkerResult::Error(err) => {25                if config.show_filesystem_errors {26                    print_error!("{}", err);27                }28                continue;29            }30        };3132        // Generate a command, execute it and store its exit code.33        let code = cmd.execute(34            dir_entry.stripped_path(config),35            config.path_separator.as_deref(),36            config.null_separator,37            buffer_output,38        );39        ret = merge_exitcodes([ret, code]);40    }41    // Returns error in case of any error.42    ret43}4445pub fn batch(46    results: impl IntoIterator<Item = WorkerResult>,47    cmd: &CommandSet,48    config: &Config,49) -> ExitCode {50    let paths = results51        .into_iter()52        .filter_map(|worker_result| match worker_result {53            WorkerResult::Entry(dir_entry) => Some(dir_entry.into_stripped_path(config)),54            WorkerResult::Error(err) => {55                if config.show_filesystem_errors {56                    print_error!("{}", err);57                }58                None59            }60        });6162    cmd.execute_batch(paths, config.batch_size, config.path_separator.as_deref())63}