93 lines
2.2 KiB
Markdown
93 lines
2.2 KiB
Markdown
# libtbr
|
|
|
|
`libtbr` is library for operating files especially for Taobin recipes. Provides a set of utilities for operations like querying, filtering, sorting, merging and more.
|
|
|
|
```
|
|
cargo add --git https://gitlab.forthrd.io/Pakin/libtbr.git
|
|
```
|
|
|
|
```
|
|
cargo update
|
|
```
|
|
|
|
## Examples
|
|
|
|
### 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
|