From 777c5ebd01cdb2d9cf0f8b915921b216c6d0932c Mon Sep 17 00:00:00 2001 From: Pakin Date: Wed, 8 Jul 2026 09:06:04 +0700 Subject: [PATCH] feat: modify recipe - add recipe01 modifier function using callbacks for easing in filtering find loop. Signed-off-by: Pakin --- src/models/recipe.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/models/recipe.rs b/src/models/recipe.rs index 4aad434..a2a1424 100644 --- a/src/models/recipe.rs +++ b/src/models/recipe.rs @@ -2,6 +2,7 @@ use std::{ collections::{BTreeMap, HashMap}, fs::File, io, + sync::atomic::AtomicBool, }; use chrono::{DateTime, NaiveDateTime}; @@ -708,6 +709,44 @@ impl Recipe { product_code_split.first().unwrap().to_string() } + + /// Modifying recipes by callback + pub fn modify_recipes( + &mut self, + cond: F, + callback: C, + ) -> Result<(), Box> + where + F: Fn(&mut Recipe01) -> bool, + C: Fn(&mut Recipe01) -> (), + { + self.Recipe01.iter_mut().for_each(|x| { + if cond(x) { + callback(x) + } + }); + Ok(()) + } + + /// Modifying recipe by callback, and only matched once. + pub fn modify_recipe_once( + &mut self, + cond: F, + callback: C, + ) -> Result<(), Box> + where + F: Fn(&mut Recipe01) -> bool, + C: Fn(&mut Recipe01) -> (), + { + let mut ab = AtomicBool::new(false); + self.Recipe01.iter_mut().for_each(|x| { + if cond(x) && !*ab.get_mut() { + callback(x); + ab.store(true, std::sync::atomic::Ordering::SeqCst); + } + }); + Ok(()) + } } // impl CommonCompressTrait for Recipe {