use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ParticipantId(pub String);
impl From<&str> for ParticipantId {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SiteId(pub String);
impl From<&str> for SiteId {
fn from(value: &str) -> Self {
Self(value.to_string())
}
}
pub trait IdentityModel {
type ParticipantId: Clone + Ord;
type SiteId: Clone + Ord;
fn sites(&self) -> Vec<Self::SiteId>;
fn site_name(&self, site: &Self::SiteId) -> String;
fn site_capabilities(&self, site: &Self::SiteId) -> BTreeSet<String>;
fn reliable_edges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)>;
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct StaticIdentityModel {
pub sites: BTreeMap<SiteId, SiteInfo>,
pub reliable_edges: BTreeSet<(SiteId, SiteId)>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SiteInfo {
pub name: String,
pub capabilities: BTreeSet<String>,
}
impl IdentityModel for StaticIdentityModel {
type ParticipantId = ParticipantId;
type SiteId = SiteId;
fn sites(&self) -> Vec<Self::SiteId> {
self.sites.keys().cloned().collect()
}
fn site_name(&self, site: &Self::SiteId) -> String {
self.sites
.get(site)
.map(|info| info.name.clone())
.unwrap_or_else(|| site.0.clone())
}
fn site_capabilities(&self, site: &Self::SiteId) -> BTreeSet<String> {
self.sites
.get(site)
.map(|info| info.capabilities.clone())
.unwrap_or_default()
}
fn reliable_edges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)> {
self.reliable_edges.clone()
}
}