From fcfae4f30e1a35edef80cdf67c1d886a50f87f96 Mon Sep 17 00:00:00 2001 From: Pakin Date: Thu, 31 Jul 2025 09:20:38 +0700 Subject: [PATCH] change fixed dir to read from config --- src/models/recipev2.rs | 2 +- src/recipe_functions/common.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/models/recipev2.rs b/src/models/recipev2.rs index 7546593..58c72a3 100644 --- a/src/models/recipev2.rs +++ b/src/models/recipev2.rs @@ -17,7 +17,7 @@ pub struct VersionRecipe { impl VersionRecipe { // import - read all file with given format from directory and mixing them together as 1 recipe pub fn create_versioning_by_country(country: &str) -> Self { - let mut path = String::from(RECIPES_DIR); + let mut path = String::from(get_recipe_dir().get("RECIPE_DIR").unwrap()); path.push_str(country); path.push('/'); diff --git a/src/recipe_functions/common.rs b/src/recipe_functions/common.rs index 32f9a6f..c7cf3fa 100644 --- a/src/recipe_functions/common.rs +++ b/src/recipe_functions/common.rs @@ -5,7 +5,31 @@ use crate::models::{self, recipe::Recipe01}; use std::collections::HashMap; use std::{fs::File, io::Read}; -pub const RECIPES_DIR: &str = "/Users/pkntd/Codes/mk4/cofffeemachineConfig/"; +// Change path here! + +fn extract_tb_config(content: String) -> HashMap { + let mut result = HashMap::new(); + let lines = content.lines(); + + for line in lines { + let parts: Vec<&str> = line.split('=').collect(); + if parts.len() == 2 { + result.insert(parts[0].trim().to_string(), parts[1].trim().to_string()); + } + } + + result +} + +/// Get config from `.tbcfg` file in current directory +pub fn get_recipe_dir() -> HashMap { + // need `.tbcfg` + let cfg_result = std::fs::read_to_string("./.tbcfg"); + match cfg_result { + Ok(cfg) => extract_tb_config(cfg), + Err(e) => panic!("Failed to read .tbcfg: {}", e), + } +} pub fn create_recipe_model_from_file(path: String) -> models::recipe::Recipe { // println!("create_recipe_model_from_file: {}", path); @@ -18,7 +42,7 @@ pub fn create_recipe_model_from_file(path: String) -> models::recipe::Recipe { } pub fn create_recipe_path(country_path: &str, version: usize) -> String { - let mut path = String::from(RECIPES_DIR); + let mut path = String::from(get_recipe_dir().get("RECIPE_DIR").unwrap()); path.push_str(country_path); path.push_str("/coffeethai02_");