wot_network/
edge.rs

1use crate::{Binding, Certificate, Identity, Regex, TrustDepth};
2
3/// An edge in the WoT network (either a [Certification] or a [Delegation])
4#[derive(Debug, Clone, Eq, Hash, PartialEq)]
5pub enum Edge {
6    Certification(Certification),
7    Delegation(Delegation),
8}
9
10impl Edge {
11    pub fn issuer(&self) -> &Certificate {
12        match self {
13            Self::Certification(certification) => certification.issuer(),
14            Self::Delegation(delegation) => delegation.issuer(),
15        }
16    }
17
18    pub fn target(&self) -> &Certificate {
19        match self {
20            Self::Certification(certification) => certification.target_cert(),
21            Self::Delegation(delegation) => delegation.target(),
22        }
23    }
24
25    pub fn is_delegation(&self) -> bool {
26        matches!(self, Edge::Delegation(..))
27    }
28
29    pub fn trust_amount(&self) -> u8 {
30        match self {
31            Self::Certification(_) => 120,
32            Self::Delegation(delegation) => delegation.trust_amount,
33        }
34    }
35
36    pub fn trust_depth(&self) -> TrustDepth {
37        match self {
38            Self::Certification(_) => TrustDepth::None,
39            Self::Delegation(delegation) => delegation.trust_depth,
40        }
41    }
42}
43
44/// A type of [Edge] that represents a certification over a binding
45#[derive(Debug, Clone, Eq, Hash, PartialEq)]
46pub struct Certification {
47    pub(crate) issuer: Certificate,
48    pub(crate) target: Binding, // FIXME: drop? - redundant with network hashmap key
49}
50
51impl Certification {
52    pub fn issuer(&self) -> &Certificate {
53        &self.issuer
54    }
55
56    pub fn target_cert(&self) -> &Certificate {
57        &self.target.cert
58    }
59    pub fn target_id(&self) -> &Identity {
60        &self.target.identity
61    }
62}
63
64/// A type of [Edge] that represents a delegation to a second certificate
65#[derive(Debug, Clone, Eq, Hash, PartialEq)]
66pub struct Delegation {
67    pub(crate) issuer: Certificate,
68    pub(crate) target: Certificate, // FIXME: drop? - redundant with network hashmap key
69    pub(crate) trust_amount: u8,
70    pub(crate) trust_depth: TrustDepth,
71    /// If any regex exists in a delegation, then at least one of the regexes must match the
72    /// target User ID for a delegation edge to be usable in a path.
73    pub(crate) regexes: Vec<Regex>,
74}
75
76impl Delegation {
77    pub fn issuer(&self) -> &Certificate {
78        &self.issuer
79    }
80
81    pub fn target(&self) -> &Certificate {
82        &self.target
83    }
84
85    pub fn trust_amount(&self) -> u8 {
86        self.trust_amount
87    }
88
89    pub fn trust_depth(&self) -> TrustDepth {
90        self.trust_depth
91    }
92
93    fn regexes(&self) -> &[Regex] {
94        &self.regexes
95    }
96
97    pub fn regex_match(&self, target_user_id: &Identity) -> bool {
98        if self.regexes().is_empty() {
99            return true;
100        }
101
102        self.regexes()
103            .iter()
104            .any(|regex| regex.matches(target_user_id))
105    }
106}