feat: add unwrap_or to entity, rewrite unwrap

This commit is contained in:
2025-09-30 14:39:58 +02:00
parent f91b755619
commit 22d58c4c5f
2 changed files with 29 additions and 2 deletions

View File

@@ -46,6 +46,24 @@ impl O {
.collect(); .collect();
items items
} }
pub fn list_by_class_and_attribute<T>(
&self,
class: &str,
attribute: &str,
value: impl Into<Value> + Clone,
) -> Vec<T>
where
T: From<Entity>,
{
let items: Vec<T> = self
.items
.values()
.filter(|item| item.class == class)
.filter(|item| item.value_of(attribute) == Some(&value.clone().into()))
.map(|item| T::from(item.clone()))
.collect();
items
}
pub fn persist(&self) { pub fn persist(&self) {
let path = path::Path::new(&self.path); let path = path::Path::new(&self.path);
sqlite::persist::catalog(path, self); sqlite::persist::catalog(path, self);

View File

@@ -59,7 +59,16 @@ impl O {
where where
T: From<Value>, T: From<Value>,
{ {
let value = self.value_of(id).unwrap(); self.value_of(id)
T::from(value.clone()) .map(|value| T::from(value.clone()))
.unwrap()
}
pub fn unwrap_or<T>(&self, id: &str, default: T) -> T
where
T: From<Value>,
{
self.value_of(id)
.map(|value| T::from(value.clone()))
.unwrap_or(default)
} }
} }