1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2023 Hugo Osvaldo Barrera
//
// SPDX-License-Identifier: EUPL-1.2

use std::sync::Arc;

use crate::{
    base::{Collection, Item, Storage},
    CollectionId, Result,
};

/// Find a collection with a matching id.
///
/// - Returns `Ok(Some(_))` if a matching collection was found.
/// - Returns `Ok(None)` if no collection has the specified id.
/// - Returns `Err(_)` if resolving the id of a collection failed.
pub(super) fn find_collection_by_id<'c, I: Item>(
    collections: &'c [Collection],
    storage: &Arc<dyn Storage<I>>,
    id: &CollectionId,
) -> Result<Option<&'c Collection>> {
    collections
        .iter()
        .find_map(|c| match storage.collection_id(c) {
            Ok(c_id) => {
                if c_id == *id {
                    Some(Ok(c))
                } else {
                    None
                }
            }
            Err(err) => Some(Err(err)),
        })
        .transpose()
}