#[cfg(doc)]
use crate::Network;
use crate::{Binding, Certificate, Identity, Regex, TrustDepth};
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum Edge {
Certification(Certification),
Delegation(Delegation),
}
impl Edge {
pub fn issuer(&self) -> &Certificate {
match self {
Self::Certification(certification) => certification.issuer(),
Self::Delegation(delegation) => delegation.issuer(),
}
}
pub fn target(&self) -> &Certificate {
match self {
Self::Certification(certification) => certification.target_cert(),
Self::Delegation(delegation) => delegation.target(),
}
}
pub fn is_delegation(&self) -> bool {
matches!(self, Edge::Delegation(..))
}
pub fn trust_amount(&self) -> u8 {
match self {
Self::Certification(_) => 120,
Self::Delegation(delegation) => delegation.trust_amount,
}
}
pub fn trust_depth(&self) -> TrustDepth {
match self {
Self::Certification(_) => TrustDepth::None,
Self::Delegation(delegation) => delegation.trust_depth,
}
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct Certification {
pub issuer: Certificate,
pub target: Binding, }
impl Certification {
pub fn issuer(&self) -> &Certificate {
&self.issuer
}
pub fn target_cert(&self) -> &Certificate {
&self.target.cert
}
pub fn target_id(&self) -> &Identity {
&self.target.identity
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct Delegation {
pub issuer: Certificate,
pub target: Certificate, pub trust_amount: u8,
pub trust_depth: TrustDepth,
pub regexes: Vec<Regex>,
}
impl Delegation {
pub fn new(
issuer: Certificate,
target: Certificate,
trust_amount: u8,
trust_depth: TrustDepth,
regexes: Vec<Regex>,
) -> Self {
Delegation {
issuer,
target,
trust_amount,
trust_depth,
regexes,
}
}
pub fn issuer(&self) -> &Certificate {
&self.issuer
}
pub fn target(&self) -> &Certificate {
&self.target
}
pub fn trust_amount(&self) -> u8 {
self.trust_amount
}
pub fn trust_depth(&self) -> TrustDepth {
self.trust_depth
}
fn regexes(&self) -> &[Regex] {
&self.regexes
}
pub fn regex_match(&self, target_user_id: &Identity) -> bool {
if self.regexes().is_empty() {
return true;
}
self.regexes()
.iter()
.any(|regex| regex.matches(target_user_id))
}
}