arch_pkg_db/multi/extend.rs
1use super::{InsertError, MultiQueryDatabase};
2use crate::value::RepositoryName;
3use arch_pkg_text::desc::{Query, QueryMut, misc::ShouldReuse};
4
5impl<'a, Querier: ShouldReuse> MultiQueryDatabase<'a, Querier> {
6 /// Extend the database with an iterator of tuples of a [`RepositoryName`] and a querier of a `desc` file.
7 fn extend_with<PairIter, Insert, InsertSuccess, InsertError>(
8 &mut self,
9 pairs: PairIter,
10 mut insert: Insert,
11 ) -> Result<(), InsertError>
12 where
13 PairIter: IntoIterator<Item = (RepositoryName<'a>, Querier)>,
14 Insert: FnMut(&mut Self, RepositoryName<'a>, Querier) -> Result<InsertSuccess, InsertError>,
15 {
16 let pairs = pairs.into_iter();
17 let (cap, _) = pairs.size_hint();
18 self.internal.reserve(cap);
19 for (repo, querier) in pairs {
20 insert(self, repo, querier)?;
21 }
22 Ok(())
23 }
24
25 /// Extend the database with an iterator of tuples of a [`RepositoryName`] and an [immutable querier](Query)
26 /// of a `desc` file.
27 ///
28 /// Old queriers which occupied the same pair of [repository](RepositoryName) and [name](arch_pkg_text::value::Name)
29 /// would be replaced.
30 pub fn extend<PairIter>(&mut self, pairs: PairIter) -> Result<(), InsertError<'_>>
31 where
32 Querier: Query<'a>,
33 PairIter: IntoIterator<Item = (RepositoryName<'a>, Querier)>,
34 {
35 self.extend_with(pairs, MultiQueryDatabase::insert)
36 }
37
38 /// Extend the database with an iterator of tuples of a [`RepositoryName`] and a [mutable querier](QueryMut)
39 /// of a `desc` file.
40 ///
41 /// Old queriers which occupied the same pair of [repository](RepositoryName) and [name](arch_pkg_text::value::Name)
42 /// would be replaced.
43 pub fn extend_mut<PairIter>(&mut self, pairs: PairIter) -> Result<(), InsertError<'_>>
44 where
45 Querier: QueryMut<'a>,
46 PairIter: IntoIterator<Item = (RepositoryName<'a>, Querier)>,
47 {
48 self.extend_with(pairs, MultiQueryDatabase::insert_mut)
49 }
50
51 /// Extend the database with an iterator of tuples of a [`RepositoryName`] and an [immutable querier](Query)
52 /// of a `desc` file.
53 ///
54 /// An item from the iterator would replace an existing entry which occupied the same pair of [repository](RepositoryName)
55 /// and [name](arch_pkg_text::value::Name) if the iterator's item has newer [version](arch_pkg_text::value::Version) than
56 /// that of the existing entry.
57 pub fn extend_newer<PairIter>(&mut self, pairs: PairIter) -> Result<(), InsertError<'_>>
58 where
59 Querier: Query<'a>,
60 PairIter: IntoIterator<Item = (RepositoryName<'a>, Querier)>,
61 {
62 self.extend_with(pairs, MultiQueryDatabase::insert_newer)
63 }
64
65 /// Extend the database with an iterator of tuples of a [`RepositoryName`] and a [mutable querier](QueryMut)
66 /// of a `desc` file.
67 ///
68 /// An item from the iterator would replace an existing entry which occupied the same pair of [repository](RepositoryName)
69 /// and [name](arch_pkg_text::value::Name) if the iterator's item has newer [version](arch_pkg_text::value::Version) than
70 /// that of the existing entry.
71 pub fn extend_newer_mut<PairIter>(&mut self, pairs: PairIter) -> Result<(), InsertError<'_>>
72 where
73 Querier: QueryMut<'a>,
74 PairIter: IntoIterator<Item = (RepositoryName<'a>, Querier)>,
75 {
76 self.extend_with(pairs, MultiQueryDatabase::insert_newer_mut)
77 }
78}