From c8351d108cfc9752821d1cd772dd00b7de039b8d Mon Sep 17 00:00:00 2001 From: Pakin Date: Wed, 10 Sep 2025 15:20:48 +0700 Subject: [PATCH] add sharding recipe, filtered by category --- src/models/recipev2.rs | 50 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/src/models/recipev2.rs b/src/models/recipev2.rs index fad97af..a0ba1a4 100644 --- a/src/models/recipev2.rs +++ b/src/models/recipev2.rs @@ -1,7 +1,7 @@ use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::io::Read; use std::path::PathBuf; use std::sync::Arc; @@ -15,7 +15,7 @@ pub struct RecipeListChange { pub raw_old: Vec, pub raw_new: Vec, /// for sub menu - pub sub_changes: HashMap, + pub sub_changes: BTreeMap, } impl RecipeListChange { @@ -73,9 +73,41 @@ impl RecipeListChange { } } +pub struct ShardingRecipe { + pub category_map: BTreeMap, +} + +impl ShardingRecipe { + pub fn build(data: BTreeMap) -> Self { + let keylist: Vec = data.keys().map(|k| k.to_string()).collect(); + let mut category_map = BTreeMap::new(); + + for key in keylist { + let kspl: Vec<&str> = key.split("-").collect(); + + if kspl.len() > 3 { + let category = kspl[1].to_string(); + + if !category_map.contains_key(&category.clone()) { + category_map.insert(category.clone(), serde_json::json!(Vec::::new())); + } + + category_map + .get_mut(&category.clone()) + .unwrap() + .as_array_mut() + .unwrap() + .push(serde_json::to_value(data.get(&key).unwrap()).unwrap()); + } + } + + Self { category_map } + } +} + /// break down recipe(s) in the folder into mapping, /// tracking diff between fields. (This does not compare other fields than Recipe01) -pub fn build_recipe_shardings(source: PathBuf) -> HashMap { +pub fn build_recipe_shardings(source: PathBuf) -> ShardingRecipe { let files = common::get_all_files_in_directory(source.as_path().to_str().unwrap()); let known_latest_version = format!("{}/version", source.as_path().to_str().unwrap()); let mut latest_version_string = String::new(); @@ -102,7 +134,7 @@ pub fn build_recipe_shardings(source: PathBuf) -> HashMap { // Structure // { productCode: { version: [ Option ] } } - let mut shards = HashMap::new(); + let mut shards = BTreeMap::new(); if recipes.is_empty() { eprintln!("recipes get empty!!!"); @@ -129,7 +161,7 @@ pub fn build_recipe_shardings(source: PathBuf) -> HashMap { .Recipe01 .par_iter() .map(|rp1| { - let mut sub_changes = HashMap::new(); + let mut sub_changes = BTreeMap::new(); if rp1.clone().SubMenu.is_some() { let subs = rp1.clone().SubMenu.unwrap().clone(); @@ -141,10 +173,10 @@ pub fn build_recipe_shardings(source: PathBuf) -> HashMap { new_value: Value::Null, raw_new: sub1.recipes.clone(), raw_old: sub1.recipes.clone(), - sub_changes: HashMap::new(), + sub_changes: BTreeMap::new(), }; - let mut change_sub_map = HashMap::new(); + let mut change_sub_map = BTreeMap::new(); change_sub_map.insert(base_version, change_sub.clone()); recipes.iter().for_each(|rc| { // clone change @@ -182,7 +214,7 @@ pub fn build_recipe_shardings(source: PathBuf) -> HashMap { sub_changes, }; - let mut change_map = HashMap::new(); + let mut change_map = BTreeMap::new(); change_map.insert(base_version, change.clone()); recipes.iter().for_each(|rc| { // clone change @@ -212,5 +244,5 @@ pub fn build_recipe_shardings(source: PathBuf) -> HashMap { }) .collect(); - shards + ShardingRecipe::build(shards) }