1use std::collections::HashSet;
6
7use crate::CollectionId;
8
9pub struct DiscoveredCollection {
11 href: String,
14 id: CollectionId,
15}
16
17impl DiscoveredCollection {
18 #[must_use]
22 pub fn new(href: String, id: CollectionId) -> DiscoveredCollection {
23 DiscoveredCollection { href, id }
24 }
25
26 #[must_use]
28 pub fn href(&self) -> &str {
29 &self.href
30 }
31
32 #[must_use]
34 pub fn id(&self) -> &CollectionId {
35 &self.id
36 }
37}
38
39pub struct Discovery {
43 collections: Vec<DiscoveredCollection>,
45}
46
47impl Discovery {
48 #[must_use]
50 pub fn collections(&self) -> &[DiscoveredCollection] {
51 &self.collections
52 }
53
54 #[must_use]
56 pub fn collection_count(&self) -> usize {
57 self.collections.len()
58 }
59
60 pub(super) fn find_collection_by_id<'disco>(
62 self: &'disco Discovery,
63 id: &CollectionId,
64 ) -> Option<&'disco DiscoveredCollection> {
65 self.collections().iter().find(|c| c.id == *id)
66 }
67}
68
69#[derive(thiserror::Error, Debug)]
70#[error("Multiple collections share the same id.")]
71pub struct DuplicateIds;
72
73impl TryFrom<Vec<DiscoveredCollection>> for Discovery {
74 type Error = DuplicateIds;
75
76 fn try_from(collections: Vec<DiscoveredCollection>) -> Result<Self, DuplicateIds> {
77 let mut seen_ids = HashSet::new();
78 for collection in &collections {
79 if !seen_ids.insert(&collection.id) {
80 return Err(DuplicateIds);
81 }
82 }
83 Ok(Discovery { collections })
84 }
85}