diff --git a/README.md b/README.md index 290495b..8eac080 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,82 @@ cargo update ## Examples -...WIP \ No newline at end of file +### Initialize Config + +```rust +use libtbr::recipe_functions::common; + +// this read file `.tbcfg` in the current directory +let cfg = common::get_config(); + +let recipe_dir = cfg.get("RECIPE_DIR").unwrap(); +``` + +--- + +** Helper functions ** + +This will be included in the next version. So the following functions may just have to implement by yourself for now. + +```rust + +use std::collections::HashMap; +use std::fs::File; +use std::io::{self, Error, Read}; + +/// Get valid country names +fn valid_country_name() -> Vec<&'static str> { + vec!["mys", "sgp", "aus", "tha", "hkg", "dubai", "uae"] +} + +/// Get latest versions of recipes +fn grep_latest_versions(dir_path: &str) -> Result, io::Error> { + let mut vs = HashMap::new(); + + // open dir + let entries = std::fs::read_dir(dir_path)? + .map(|res| res.map(|e| e.path())) + .collect::, io::Error>>()?; + + for e in entries { + let path = e.clone().to_str().unwrap().to_string(); + let mut cl_path = path.clone(); + let path_split = path.split("/").collect::>(); + let dir_name = path_split[path_split.len() - 1]; + + if valid_country_name().contains(&dir_name) { + cl_path.push_str("/version"); + + // read filename + let mut file = File::open(cl_path)?; + let mut data = String::new(); + file.read_to_string(&mut data).unwrap(); + + vs.insert(dir_name.to_string(), data.parse::().unwrap()); + } + + // expect dir with country + } + + Ok(vs) +} + +``` + +### Get recipe from specific country (latest) +```rust +... +let latest_versions = grep_latest_versions(recipe_dir).unwrap(); + +// try get malaysia recipe, may fail if country does not exist +let mys_version = latest_versions.get("mys"); +// try to create malaysia recipe model +let mys_recipe_model = common::create_recipe_model_from_file(common::create_recipe_path( + "mys", + *mys_version.unwrap(), +)); + +... +``` + +...WIP