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#[derive(Debug, Clone, Eq, Hash, PartialEq)]
45pub struct Certification {
46 pub(crate) issuer: CertId,
47 pub(crate) target: CertId, pub(crate) user_id: UserId, 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#[derive(Debug, Clone, Eq, Hash, PartialEq)]
67pub struct Delegation {
68 pub(crate) issuer: CertId,
69 pub(crate) target: CertId, pub(crate) trust_amount: u8,
71 pub(crate) trust_depth: TrustDepth,
72 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}