1use std::collections::{BTreeMap, BTreeSet};
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9pub struct ParticipantId(pub String);
10
11impl From<&str> for ParticipantId {
12 fn from(value: &str) -> Self {
13 Self(value.to_string())
14 }
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
19pub struct SiteId(pub String);
20
21impl From<&str> for SiteId {
22 fn from(value: &str) -> Self {
23 Self(value.to_string())
24 }
25}
26
27pub trait IdentityModel {
29 type ParticipantId: Clone + Ord;
31 type SiteId: Clone + Ord;
33
34 fn sites(&self) -> Vec<Self::SiteId>;
36 fn site_name(&self, site: &Self::SiteId) -> String;
38 fn site_capabilities(&self, site: &Self::SiteId) -> BTreeSet<String>;
40 fn reliable_edges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)>;
42
43 #[allow(non_snake_case)]
45 fn siteName(&self, site: &Self::SiteId) -> String {
46 self.site_name(site)
47 }
48
49 #[allow(non_snake_case)]
51 fn siteCapabilities(&self, site: &Self::SiteId) -> BTreeSet<String> {
52 self.site_capabilities(site)
53 }
54
55 #[allow(non_snake_case)]
57 fn reliableEdges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)> {
58 self.reliable_edges()
59 }
60}
61
62#[derive(Debug, Clone, Default, Serialize, Deserialize)]
64pub struct StaticIdentityModel {
65 pub sites: BTreeMap<SiteId, SiteInfo>,
67 pub reliable_edges: BTreeSet<(SiteId, SiteId)>,
69}
70
71#[derive(Debug, Clone, Default, Serialize, Deserialize)]
73pub struct SiteInfo {
74 pub name: String,
76 pub capabilities: BTreeSet<String>,
78}
79
80impl IdentityModel for StaticIdentityModel {
81 type ParticipantId = ParticipantId;
82 type SiteId = SiteId;
83
84 fn sites(&self) -> Vec<Self::SiteId> {
85 self.sites.keys().cloned().collect()
86 }
87
88 fn site_name(&self, site: &Self::SiteId) -> String {
89 self.sites
90 .get(site)
91 .map(|info| info.name.clone())
92 .unwrap_or_else(|| site.0.clone())
93 }
94
95 fn site_capabilities(&self, site: &Self::SiteId) -> BTreeSet<String> {
96 self.sites
97 .get(site)
98 .map(|info| info.capabilities.clone())
99 .unwrap_or_default()
100 }
101
102 fn reliable_edges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)> {
103 self.reliable_edges.clone()
104 }
105}