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