use crate::item::Item; /// Item database pub struct ItemDB { index: mdq::Index, } 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, true); for item in &index.documents { let item = Item::new(item); log::info!("Adding item {} to DB", item.name); } Self { index } } /// Retrieves an item by name pub fn get_item(&self, item: &str) -> Option { Some( self.index .documents .iter() .map(Item::new) // <-- todo : performance? .find(|x| x.name == item)?, ) } /// Get all items pub fn items(&self) -> Vec { let mut ret = vec![]; for item in &self.index.documents { let item = Item::new(item); ret.push(item.name); } ret } }