feat: simplify return type of catalog.list method

This commit is contained in:
2026-02-27 10:44:28 +01:00
parent cdf5ead07d
commit 68c6ca4e9f
6 changed files with 4 additions and 9 deletions

View File

@@ -134,7 +134,6 @@ fn main() {
.list::<Component>() .list::<Component>()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|c| c.unwrap())
.collect(); .collect();
// Print the loaded components // Print the loaded components
println!( println!(

View File

@@ -145,7 +145,6 @@ fn main() -> Result<(), FailedTo> {
.list() .list()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|p| p.unwrap())
.collect(); .collect();
println!("✅ Loaded {} laptop(s) using filter.", laptops.len()); println!("✅ Loaded {} laptop(s) using filter.", laptops.len());
@@ -173,7 +172,6 @@ fn main() -> Result<(), FailedTo> {
.list() .list()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|p| p.unwrap())
.collect(); .collect();
println!( println!(
@@ -205,7 +203,6 @@ fn main() -> Result<(), FailedTo> {
.list() .list()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|p| p.unwrap())
.collect(); .collect();
println!("✅ Loaded {} total products.", all_products.len()); println!("✅ Loaded {} total products.", all_products.len());

View File

View File

@@ -20,7 +20,7 @@ impl Catalog {
/// ///
/// Returns `Err(FailedTo::LockCatalog)` if the catalog's internal mutex /// Returns `Err(FailedTo::LockCatalog)` if the catalog's internal mutex
/// could not be locked. /// could not be locked.
pub fn list<T>(&self) -> Result<Vec<Result<T, FailedTo>>, FailedTo> pub fn list<T>(&self) -> Result<Vec<T>, FailedTo>
where where
T: EAV, T: EAV,
{ {
@@ -28,7 +28,7 @@ impl Catalog {
Ok(items Ok(items
.values() .values()
.filter(move |item| item.class == T::class()) .filter(move |item| item.class == T::class())
.map(|item| T::try_from(item.clone()).map_err(|_| FailedTo::ConvertEntity)) .filter_map(|item| T::try_from(item.clone()).ok())
.collect()) .collect())
}) })
} }

View File

@@ -74,7 +74,6 @@ mod tests {
.list::<Item>() .list::<Item>()
.unwrap() .unwrap()
.into_iter() .into_iter()
.map(|item| item.unwrap())
.collect(); .collect();
// Sort by ID to ensure consistent order for comparison // Sort by ID to ensure consistent order for comparison
let mut expected_items = items_to_insert; let mut expected_items = items_to_insert;

View File

@@ -27,8 +27,8 @@ mod tests {
let _ = catalog.upsert(item2.clone()); let _ = catalog.upsert(item2.clone());
let results = catalog.list::<Item>().unwrap(); let results = catalog.list::<Item>().unwrap();
assert_eq!(results.len(), 2); assert_eq!(results.len(), 2);
assert!(results.contains(&Ok(item1))); assert!(results.contains(&item1));
assert!(results.contains(&Ok(item2))); assert!(results.contains(&item2));
} }
#[test] #[test]
fn list_by_class_should_return_empty_iterator_if_no_match() { fn list_by_class_should_return_empty_iterator_if_no_match() {