wot_network/
network.rs

1use std::collections::HashMap;
2
3use crate::{Binding, CertId, Certification, Delegation, Regex, TrustDepth, UserId};
4
5/// A "Web of Trust" network consisting of [Certification]s and [Delegation]s.
6///
7/// A [Network] represents a snapshot of valid nodes and edges in a set of OpenPGP Certificates
8/// at a reference time.
9///
10///
11/// **NOTE**:
12///
13/// Users have to create the network and ensure validity of all nodes and edges themselves at the
14/// reference time themselves. Users also have to filter out all edges that are not in effect for a
15/// given reference time.
16#[derive(Debug, PartialEq)]
17pub struct Network {
18    // Certifications on a binding
19    pub certifications: HashMap<Binding, Vec<Certification>>,
20
21    // Delegations on a cert
22    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        // FIXME: less CertId cloning!?
51
52        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            // add edge to pre-existing EdgeSet
64            edges.push(edge);
65        } else {
66            // make a new EdgeSet, put this edge in
67            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        // FIXME: less CertId cloning!?
88
89        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            // Add edge to pre-existing EdgeSet
99            edges.push(edge);
100        } else {
101            // Make a new EdgeSet
102            let list = vec![edge];
103
104            // Add this EdgeSet to the Network
105            self.delegations.insert(target_cert, list);
106        }
107    }
108}