This commit is contained in:
JMARyA 2024-09-23 09:26:11 +02:00
parent 7f32f2429e
commit 65bfb5f8e2
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
10 changed files with 60 additions and 55 deletions

View file

@ -1,10 +1,12 @@
use std::collections::HashMap;
use mongod::Model;
use crate::item::Item;
/// Item database
pub struct ItemDB {
index: mdq::Index,
index: HashMap<String, Item>,
}
impl ItemDB {
@ -13,34 +15,27 @@ impl ItemDB {
/// 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);
let index = mdq::Index::new(dir, false);
let mut items = HashMap::new();
for item in &index.documents {
let item = Item::new(item);
item.insert_overwrite().await.unwrap();
log::info!("Adding item {} to DB", item.name);
items.insert(item._id.clone(), item);
}
Self { index }
Self { index: items }
}
/// Retrieves an item by name
pub fn get_item(&self, item: &str) -> Option<Item> {
self.index
.documents
.iter()
.map(Item::new) // <-- todo : performance?
.find(|x| x._id == item)
pub fn get_item(&self, item: &str) -> Option<&Item> {
self.index.get(item)
}
/// Get all items
pub fn items(&self) -> Vec<String> {
let mut ret = vec![];
for item in &self.index.documents {
let item = Item::new(item);
ret.push(item._id.clone());
}
ret
pub fn items(&self) -> Vec<&String> {
self.index.keys().collect()
}
}

View file

@ -80,5 +80,5 @@ pub fn verify_item_variant_exists(item_variant: &str, id: &str, itemdb: &ItemDB)
return Some(true);
}
return None;
None
}

View file

@ -35,9 +35,7 @@ impl Validate for Location {
impl Location {
/// Recursively get the conditions of a location. This inherits from parent locations.
pub fn conditions_rec<'a>(
&'a self,
) -> futures::future::BoxFuture<'a, Option<StorageConditions>> {
pub fn conditions_rec(&self) -> futures::future::BoxFuture<'_, Option<StorageConditions>> {
async move {
if let Some(cond) = &self.conditions {
return Some(cond.clone());
@ -64,7 +62,7 @@ impl Location {
}
// Get all children locations
pub fn children_recursive<'a>(&'a self) -> futures::future::BoxFuture<'a, Vec<Location>> {
pub fn children_recursive(&self) -> futures::future::BoxFuture<'_, Vec<Location>> {
async move {
let mut all = Vec::new();

View file

@ -185,6 +185,7 @@ impl mongod::ToAPI for Transaction {
"variant": self.variant,
"price": self.price,
"origin": self.origin,
"location": self.location.as_ref().map(|x| x.id().to_string()),
"timestamp": self.timestamp,
"consumed": self.consumed,
"note": self.note,

View file

@ -305,7 +305,7 @@ pub fn unique_flows(i: &[String]) -> Vec<String> {
if let Some(suffix) = s.strip_prefix("flow::") {
// Extract the part after "flow::" and split on "::" to get the kind (ignoring id)
let parts: Vec<&str> = suffix.split("::").collect();
if let Some(kind) = parts.get(0) {
if let Some(kind) = parts.first() {
// Build the common prefix "flow::kind"
let common_prefix = format!("flow::{}", kind);