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 {