wot_network/
network.rs

1use std::collections::HashMap;
2
3use crate::{Binding, Certificate, Certification, Delegation, Regex, TrustDepth};
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 will usually want 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///
16/// One implementation of formation of `wot-network` graphs is available in
17/// [wot-network-rpgpie](https://crates.io/crates/wot-network-rpgpie).
18#[derive(Debug, PartialEq, Clone)]
19pub struct Network {
20    // Certifications on a binding
21    pub certifications: HashMap<Binding, Vec<Certification>>,
22
23    // Delegations on a cert
24    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        // FIXME: less cloning!?
47
48        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            // add edge to pre-existing EdgeSet
58            edges.push(edge);
59        } else {
60            // make a new EdgeSet, put this edge in
61            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        // FIXME: less Certificate cloning!?
82
83        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            // Add edge to pre-existing EdgeSet
93            edges.push(edge);
94        } else {
95            // Make a new EdgeSet
96            let list = vec![edge];
97
98            // Add this EdgeSet to the Network
99            self.delegations.insert(target_cert, list);
100        }
101    }
102}