feat: modify recipe

- add recipe01 modifier function using callbacks for easing in filtering find loop.

Signed-off-by: Pakin <pakin.t@forth.co.th>
This commit is contained in:
Pakin 2026-07-08 09:06:04 +07:00
parent 2001888cd2
commit 777c5ebd01

View file

@ -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<F, C>(
&mut self,
cond: F,
callback: C,
) -> Result<(), Box<dyn std::error::Error>>
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<F, C>(
&mut self,
cond: F,
callback: C,
) -> Result<(), Box<dyn std::error::Error>>
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 {