wot_network/
edge.rs

1use crate::{Certificate, Regex, TrustDepth, Identity};
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(),
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: Certificate, // FIXME: drop? - redundant with network hashmap key
49    pub(crate) user_id: Identity, // FIXME: drop? - redundant with network hashmap key
50}
51
52impl Certification {
53    pub fn issuer(&self) -> &Certificate {
54        &self.issuer
55    }
56
57    pub fn target(&self) -> &Certificate {
58        &self.target
59    }
60    pub fn user_id(&self) -> &Identity {
61        &self.user_id
62    }
63}
64
65/// A type of [Edge] that represents a delegation to a second certificate
66#[derive(Debug, Clone, Eq, Hash, PartialEq)]
67pub struct Delegation {
68    pub(crate) issuer: Certificate,
69    pub(crate) target: Certificate, // FIXME: drop? - redundant with network hashmap key
70    pub(crate) trust_amount: u8,
71    pub(crate) trust_depth: TrustDepth,
72    /// If any regex exists in a delegation, then at least one of the regexes must match the
73    /// target User ID for a delegation edge to be usable in a path.
74    pub(crate) regexes: Vec<Regex>,
75}
76
77impl Delegation {
78    pub fn issuer(&self) -> &Certificate {
79        &self.issuer
80    }
81
82    pub fn target(&self) -> &Certificate {
83        &self.target
84    }
85
86    pub fn trust_amount(&self) -> u8 {
87        self.trust_amount
88    }
89
90    pub fn trust_depth(&self) -> TrustDepth {
91        self.trust_depth
92    }
93
94    fn regexes(&self) -> &[Regex] {
95        &self.regexes
96    }
97
98    pub fn regex_match(&self, target_user_id: &Identity) -> bool {
99        if self.regexes().is_empty() {
100            return true;
101        }
102
103        self.regexes()
104            .iter()
105            .any(|regex| regex.matches(target_user_id))
106    }
107}