1use std::collections::HashMap;
2
3use crate::{Binding, Certificate, Certification, Delegation, Regex, TrustDepth};
4
5#[derive(Debug, PartialEq, Clone)]
19pub struct Network {
20 pub certifications: HashMap<Binding, Vec<Certification>>,
22
23 pub delegations: HashMap<Certificate, Vec<Delegation>>,
25}
26
27impl Default for Network {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl Network {
34 pub fn new() -> Self {
35 Self {
36 certifications: Default::default(),
37 delegations: Default::default(),
38 }
39 }
40
41 pub fn num_edges(&self) -> usize {
42 self.certifications.len() + self.delegations.len()
43 }
44
45 pub fn add_binding(&mut self, issuer: Certificate, target: Binding) {
46 let edge = Certification {
49 issuer: issuer.clone(),
50 target: target.clone(),
51 };
52
53 if let Some(edges) = self.certifications.get_mut(&Binding {
54 cert: target.cert.clone(),
55 identity: target.identity.clone(),
56 }) {
57 edges.push(edge);
59 } else {
60 let list = vec![edge];
62
63 self.certifications.insert(
64 Binding {
65 cert: target.cert,
66 identity: target.identity,
67 },
68 list,
69 );
70 }
71 }
72
73 pub fn add_delegation(
74 &mut self,
75 issuer: Certificate,
76 target_cert: Certificate,
77 trust_amount: u8,
78 trust_depth: TrustDepth,
79 regexes: Vec<Regex>,
80 ) {
81 let edge = Delegation {
84 issuer: issuer.clone(),
85 target: target_cert.clone(),
86 trust_amount,
87 trust_depth,
88 regexes,
89 };
90
91 if let Some(edges) = self.delegations.get_mut(&target_cert) {
92 edges.push(edge);
94 } else {
95 let list = vec![edge];
97
98 self.delegations.insert(target_cert, list);
100 }
101 }
102}