pogo_masterfile/accessor/
code_gate_proto.rs1use std::collections::HashMap;
4
5use pogo_masterfile_types::{
6 MasterfileEntry,
7 code_gate_proto::{CodeGateProtoEntry, CodeGateProtoTemplateId},
8};
9
10pub struct CodeGateProtoAccessor<'a> {
11 pub(crate) entries: &'a [MasterfileEntry],
12 pub(crate) index: &'a HashMap<CodeGateProtoTemplateId, usize>,
13 pub(crate) order: &'a [usize],
14}
15
16impl<'a> CodeGateProtoAccessor<'a> {
17 pub fn get<I>(&self, id: I) -> Option<&'a CodeGateProtoEntry>
22 where
23 I: TryInto<CodeGateProtoTemplateId>,
24 {
25 let typed = id.try_into().ok()?;
26 let idx = *self.index.get(&typed)?;
27 match &self.entries[idx] {
28 MasterfileEntry::CodeGateProto(e) => Some(e),
29 _ => None,
30 }
31 }
32
33 pub fn has<I>(&self, id: I) -> bool
34 where
35 I: TryInto<CodeGateProtoTemplateId>,
36 {
37 self.get(id).is_some()
38 }
39
40 pub fn iter(&self) -> impl Iterator<Item = &'a CodeGateProtoEntry> + '_ {
41 self.order
42 .iter()
43 .filter_map(|&idx| match &self.entries[idx] {
44 MasterfileEntry::CodeGateProto(e) => Some(e),
45 _ => None,
46 })
47 }
48
49 pub fn template_ids(&self) -> impl Iterator<Item = CodeGateProtoTemplateId> + '_ {
50 self.index.keys().copied()
51 }
52
53 pub fn len(&self) -> usize {
54 self.order.len()
55 }
56
57 pub fn is_empty(&self) -> bool {
58 self.order.is_empty()
59 }
60}