so6/
lib.rs

1use crate::index::IndexDefinition;
2use crate::index::IndexHandler;
3use glob::glob;
4use serde_yaml;
5use std::{collections::HashMap, fs, path::Path, path::PathBuf};
6
7pub mod database;
8mod index;
9
10pub fn list<P: AsRef<Path>>(prj_path: PathBuf, glob_pattern: P) -> Vec<IndexHandler> {
11    let mut indexes: Vec<IndexHandler> = [].to_vec();
12    for index_file in
13        glob(prj_path.join(glob_pattern).to_str().unwrap()).expect("No So6 index file found")
14    {
15        let index_file_string = index_file.unwrap();
16        let index_definitions: HashMap<String, IndexDefinition> = serde_yaml::from_str(
17            fs::read_to_string(index_file_string.to_str().unwrap())
18                .unwrap()
19                .as_str(),
20        )
21        .expect(
22            format!(
23                "Couldn't deserialize {}",
24                index_file_string.to_str().unwrap()
25            )
26            .as_str(),
27        );
28        for (index_creation_name, index_definition) in index_definitions.iter() {
29            let index_handler =
30                IndexHandler::new(index_creation_name.to_string(), index_definition.clone());
31            if index_handler.required() {
32                indexes.push(index_handler);
33            }
34        }
35    }
36    indexes.sort_by_key(|ih| ih.def.name.clone());
37    indexes
38}