From 72533ea8e86b76042cd3f2884b1f5e92226f2317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Thu, 20 Jul 2023 16:38:02 +0200 Subject: [PATCH] refactor: remove duplicate code on Config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christina Sørensen --- src/git.rs | 94 ++++++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/src/git.rs b/src/git.rs index d4eb97e..df2d5e5 100644 --- a/src/git.rs +++ b/src/git.rs @@ -317,46 +317,14 @@ impl Repo { } } +// pub fn all_on_all(&self, closures: Vec, break_on_err: bool) { + macro_rules! run_series { ($conf:ident, $closures:ident) => { - for category in $conf.categories.values() { - for (_, repo) in category.repos.as_ref().expect("failed to get repos").iter() { - use RepoKinds::*; - match &repo.kind { - Some(GitRepo) => { - for instruction in &$closures { - let f = &instruction.closure; - let op = instruction.operation; - if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) { - let mut sp = Spinner::new( - Spinners::Dots10, - format!("{}: {}", repo.name, op), - ); - if f(repo) { - sp.stop_and_persist( - success_str(), - format!("{}: {}", repo.name, op), - ); - } else { - sp.stop_and_persist( - failure_str(), - format!("{}: {}", repo.name, op), - ); - } - } else { - f(repo); - } - } - } - None => { - println!("unknown kind {:?}", repo.kind) - } - Some(kind) => { - println!("unknown kind {kind:?}") - } - } - } - } + $conf.all_on_all($closures, false); + }; + ($conf:ident, $closures:ident, $stop_on_err:tt) => { + $conf.all_on_all($closures, $stop_on_err); }; } @@ -522,7 +490,7 @@ impl Config { sp.stop_and_persist(success_str(), format!("{}: {}", repo.name, op)); } else { sp.stop_and_persist(failure_str(), format!("{}: {}", repo.name, op)); - break; + break; // NOTE: the difference :D } } else { f(repo); @@ -562,22 +530,44 @@ impl Config { /// }, /// ]; /// ``` - pub fn all_on_all(&self, closures: Vec) { + pub fn all_on_all(&self, closures: Vec, break_on_err: bool) { for category in self.categories.values() { for (_, repo) in category.repos.as_ref().expect("failed to get repos").iter() { - for instruction in &closures { - let f = &instruction.closure; - let op = instruction.operation; - if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) { - let mut sp = - Spinner::new(Spinners::Dots10, format!("{}: {}", repo.name, op)); - if f(repo) { - sp.stop_and_persist(success_str(), format!("{}: {}", repo.name, op)); - } else { - sp.stop_and_persist(failure_str(), format!("{}: {}", repo.name, op)); + use RepoKinds::*; + match &repo.kind { + Some(GitRepo) => { + for instruction in &closures { + let f = &instruction.closure; + let op = instruction.operation; + if !settings::QUIET.load(std::sync::atomic::Ordering::Relaxed) { + let mut sp = Spinner::new( + Spinners::Dots10, + format!("{}: {}", repo.name, op), + ); + if f(repo) { + sp.stop_and_persist( + success_str(), + format!("{}: {}", repo.name, op), + ); + } else { + sp.stop_and_persist( + failure_str(), + format!("{}: {}", repo.name, op), + ); + if break_on_err { + break; + } + } + } else { + f(repo); + } } - } else { - f(repo); + } + None => { + println!("unknown kind {:?}", repo.kind) + } + Some(kind) => { + println!("unknown kind {kind:?}") } } }