wot_network/
network.rs

1use std::collections::HashMap;
2
3use crate::{Binding, Certificate, Certification, Delegation, Regex, TrustDepth, Identity};
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/// **NOTE**:
11///
12/// Users of this crate are expected to create a `wot-network` graph from OpenPGP certificates.
13/// This involves applying all relevant OpenPGP semantics and forming a network of exactly
14/// those nodes and edges that are valid and active at the reference time.
15#[derive(Debug, PartialEq)]
16pub struct Network {
17    // Certifications on a binding
18    pub certifications: HashMap<Binding, Vec<Certification>>,
19
20    // Delegations on a cert
21    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        // FIXME: less Certificate cloning!?
49
50        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            // add edge to pre-existing EdgeSet
61            edges.push(edge);
62        } else {
63            // make a new EdgeSet, put this edge in
64            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        // FIXME: less Certificate cloning!?
85
86        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            // Add edge to pre-existing EdgeSet
96            edges.push(edge);
97        } else {
98            // Make a new EdgeSet
99            let list = vec![edge];
100
101            // Add this EdgeSet to the Network
102            self.delegations.insert(target_cert, list);
103        }
104    }
105}