libtbr/src/recipe_functions/list_fields.rs
2025-05-26 12:39:09 +07:00

122 lines
3.5 KiB
Rust

use crate::models::recipe::{Recipe, Recipe01};
impl Recipe {
/// The function `list_menu_product_code` returns a vector of product codes from a list of recipes.
///
/// Returns:
///
/// a vector of string references (`&str`) representing the product codes of the recipes in
/// `Recipe01`.
pub fn list_menu_product_code(&self) -> Vec<&str> {
self.Recipe01
.iter()
.map(|r| r.productCode.as_str())
.collect()
}
/// The function `list_material_settings` returns a vector of strings containing the IDs of material
/// settings.
///
/// Returns:
///
/// The function `list_material_settings` returns a `Vec<String>` which contains the IDs of the material
/// settings.
pub fn list_material_settings(&self) -> Vec<String> {
self.MaterialSetting
.iter()
.map(|r| r.id.to_string())
.collect()
}
pub fn list_topping_list(&self) -> Vec<String> {
self.Topping
.ToppingList
.iter()
.map(|r| r.id.clone().to_string())
.collect()
}
pub fn list_topping_group(&self) -> Vec<String> {
self.Topping
.ToppingGroup
.iter()
.map(|r| r.groupID.clone().to_string())
.collect()
}
#[cfg(feature = "diff")]
pub fn list_diff_pd_between_recipes(&self, another_recipe: &Recipe) -> Vec<String> {
let mut list = Vec::new();
self.Recipe01.iter().for_each(|r| {
if !another_recipe.diff_pd_between_recipes(another_recipe, r.productCode.clone()) {
list.push(r.productCode.clone());
}
});
list
}
pub fn list_diff_pd_ignore_country_code(&self, another_recipe: &Recipe) -> Vec<String> {
let mut list = Vec::new();
self.Recipe01.iter().for_each(|r| {
// clean before search
let rpl = r.clone();
// grep only recipe without country code
let pd = rpl.productCode;
let pd_split = pd.split("-").collect::<Vec<&str>>();
// combine without first elem
let mut new_pd = String::new();
for (i, ele) in pd_split.iter().enumerate() {
if i != 0 {
new_pd.push_str(ele);
if i != pd_split.len() - 1 {
new_pd.push('-');
}
}
}
if !another_recipe
.diff_pd_between_recipes_ignore_country(another_recipe, new_pd.clone())
{
list.push(new_pd.clone());
}
});
list
}
pub fn find_recipe_by_material_path_id(&self, material_path_id: &str) -> Vec<&Recipe01> {
// let mut res = Vec::new();
let total: Vec<&Recipe01> = self
.Recipe01
.iter()
.filter(|r| {
for ele in r.recipes.clone() {
if ele.materialPathId.as_i64().unwrap()
== material_path_id.parse::<i64>().unwrap()
{
// res.push(r.clone());
return true;
}
}
return false;
})
.collect();
total
}
}
impl Recipe01 {
pub fn list_recipe_only_id(&self) -> Vec<String> {
self.recipes
.iter()
.map(|rpl| rpl.materialPathId.clone().to_string())
.collect()
}
}