use crate::item::{Item, ItemEntry}; #[macro_export] macro_rules! collect_results { ($res:expr) => {{ use futures::stream::TryStreamExt; let mut ret = vec![]; while let Some(doc) = $res.try_next().await.unwrap() { ret.push(doc); } ret }}; } #[macro_export] macro_rules! cdb_col { ($db:expr, $col:expr) => { $db.database("cdb") .collection::($col) }; } #[macro_export] macro_rules! get_mongo { () => { mongodb::Client::with_uri_str(std::env::var("DB_URI").unwrap()) .await .unwrap() }; } #[macro_export] macro_rules! id_of { ($id:expr) => { doc! { "_id": $id} }; } pub struct ItemDB { index: mdq::Index, } impl ItemDB { pub async fn new(dir: &str) -> Self { // scan for markdown item entries let index = mdq::Index::new(dir, true); let mongodb = get_mongo!(); for item in &index.documents { let item = ItemEntry::new(item.clone()); item.init_db(&mongodb).await; } Self { index } } /// Retrieves an item by name pub fn get_item(&self, item: &str) -> Option { Some(Item::new( self.index .documents .iter() .map(|x| ItemEntry::new(x.clone())) .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 = ItemEntry::new(item.clone()); ret.push(item.name); } ret } }