wot_network/
edge.rs

1use crate::{CertId, Regex, TrustDepth, UserId};
2
3#[derive(Debug, Clone, Eq, Hash, PartialEq)]
4pub enum Edge {
5    Certification(Certification),
6    Delegation(Delegation),
7}
8
9impl Edge {
10    pub fn issuer(&self) -> &CertId {
11        match self {
12            Self::Certification(certification) => certification.issuer(),
13            Self::Delegation(delegation) => delegation.issuer(),
14        }
15    }
16
17    pub fn target(&self) -> &CertId {
18        match self {
19            Self::Certification(certification) => certification.target(),
20            Self::Delegation(delegation) => delegation.target(),
21        }
22    }
23
24    pub fn is_delegation(&self) -> bool {
25        matches!(self, Edge::Delegation(..))
26    }
27
28    pub fn trust_amount(&self) -> u8 {
29        match self {
30            Self::Certification(certification) => certification.trust_amount,
31            Self::Delegation(delegation) => delegation.trust_amount,
32        }
33    }
34
35    pub fn trust_depth(&self) -> TrustDepth {
36        match self {
37            Self::Certification(_) => TrustDepth::None,
38            Self::Delegation(delegation) => delegation.trust_depth,
39        }
40    }
41}
42
43/// An individual binding certification in the graph
44#[derive(Debug, Clone, Eq, Hash, PartialEq)]
45pub struct Certification {
46    pub(crate) issuer: CertId,
47    pub(crate) target: CertId, // FIXME: drop? - redundant with network hashmap key
48    pub(crate) user_id: UserId, // FIXME: drop? - redundant with network hashmap key
49    pub(crate) trust_amount: u8,
50}
51
52impl Certification {
53    pub fn issuer(&self) -> &CertId {
54        &self.issuer
55    }
56
57    pub fn target(&self) -> &CertId {
58        &self.target
59    }
60    pub fn user_id(&self) -> &UserId {
61        &self.user_id
62    }
63}
64
65/// An individual delegation in the graph
66#[derive(Debug, Clone, Eq, Hash, PartialEq)]
67pub struct Delegation {
68    pub(crate) issuer: CertId,
69    pub(crate) target: CertId, // 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) -> &CertId {
79        &self.issuer
80    }
81
82    pub fn target(&self) -> &CertId {
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: &UserId) -> 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}