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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::collections::HashMap;
pub use error::*;
use holochain_zome_types::EntryDefIndex;
use holochain_zome_types::LinkType;
use holochain_zome_types::ScopedZomeTypes;
use holochain_zome_types::ScopedZomeTypesSet;
use holochain_zome_types::ZomeId;
#[allow(missing_docs)]
mod error;
#[cfg(test)]
mod test;
pub type NumZomeTypes = u8;
#[derive(Clone, Debug, PartialEq, Default)]
pub struct GlobalZomeTypes {
entries: HashMap<ZomeId, NumZomeTypes>,
links: HashMap<ZomeId, NumZomeTypes>,
}
impl GlobalZomeTypes {
pub fn from_ordered_iterator<I>(ordered_iterator: I) -> ZomeTypesResult<GlobalZomeTypes>
where
I: IntoIterator<Item = (EntryDefIndex, LinkType)>,
{
let r = ordered_iterator.into_iter().enumerate().try_fold(
Self::default(),
|mut zome_types, (zome_id, (num_entry_types, num_link_types))| {
let zome_id: ZomeId = u8::try_from(zome_id)
.map_err(|_| ZomeTypesError::ZomeIndexOverflow)?
.into();
zome_types.entries.insert(zome_id, num_entry_types.0);
zome_types.links.insert(zome_id, num_link_types.0);
Ok(zome_types)
},
)?;
Ok(r)
}
pub fn in_scope_subset(&self, zomes: &[ZomeId]) -> ScopedZomeTypesSet {
let entries = zomes
.iter()
.filter_map(|zome_id| self.entries.get_key_value(zome_id).map(|(z, l)| (*z, *l)));
let entries = new_scope(entries);
let links = zomes
.iter()
.filter_map(|zome_id| self.links.get_key_value(zome_id).map(|(z, l)| (*z, *l)));
let links = new_scope(links);
ScopedZomeTypesSet { entries, links }
}
}
fn new_scope<T>(iter: impl Iterator<Item = (ZomeId, NumZomeTypes)>) -> ScopedZomeTypes<T>
where
T: From<u8>,
{
let iter = iter
.map(|(zome_id, len)| (zome_id, (0..len).map(Into::into).collect()))
.collect();
ScopedZomeTypes(iter)
}