add examples
This commit is contained in:
parent
7769fbf8b6
commit
64a7dae017
1 changed files with 79 additions and 1 deletions
80
README.md
80
README.md
|
|
@ -12,4 +12,82 @@ cargo update
|
|||
|
||||
## Examples
|
||||
|
||||
...WIP
|
||||
### 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<HashMap<String, usize>, 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::<Result<Vec<_>, 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::<Vec<&str>>();
|
||||
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::<usize>().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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue