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