use std::collections::HashMap; use crate::item::Item; /// Item database #[derive(Debug, Clone)] pub struct ItemDB { index: HashMap, } impl ItemDB { /// Create a new item database using `dir` as the base. /// /// The directory should contain markdown documents with valid frontmatter to be parsed into `Item`s pub async fn new(dir: &str) -> Self { // scan for markdown item entries let index = mdq::Index::new(dir, false); let mut items = HashMap::new(); for item in &index.documents { let item = Item::new(item); items.insert(item.id.clone(), item); } Self { index: items } } /// Retrieves an item by name pub fn get_item(&self, item: &str) -> Option<&Item> { self.index.get(item) } /// Get all items pub fn items(&self) -> Vec<&String> { self.index.keys().collect() } } /// Get all item variants which inventory is under the minimum. Returns a Vec with the item variants id and the missing quanity to reach minimum. pub async fn get_items_without_min_satisfied(db: &ItemDB) -> Vec<(String, i64)> { let mut ret = Vec::new(); for item in db.items() { let item = db.get_item(&item).unwrap(); for var in &item.variants { let res = var.1.is_below_min().await; if res.0 { ret.push((format!("{}::{}", var.1.item, var.1.variant), res.1)); } } } ret }