telltale_machine/
identity.rs1use 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
44#[derive(Debug, Clone, Default, Serialize, Deserialize)]
46pub struct StaticIdentityModel {
47 pub sites: BTreeMap<SiteId, SiteInfo>,
49 pub reliable_edges: BTreeSet<(SiteId, SiteId)>,
51}
52
53#[derive(Debug, Clone, Default, Serialize, Deserialize)]
55pub struct SiteInfo {
56 pub name: String,
58 pub capabilities: BTreeSet<String>,
60}
61
62impl IdentityModel for StaticIdentityModel {
63 type ParticipantId = ParticipantId;
64 type SiteId = SiteId;
65
66 fn sites(&self) -> Vec<Self::SiteId> {
67 self.sites.keys().cloned().collect()
68 }
69
70 fn site_name(&self, site: &Self::SiteId) -> String {
71 self.sites
72 .get(site)
73 .map(|info| info.name.clone())
74 .unwrap_or_else(|| site.0.clone())
75 }
76
77 fn site_capabilities(&self, site: &Self::SiteId) -> BTreeSet<String> {
78 self.sites
79 .get(site)
80 .map(|info| info.capabilities.clone())
81 .unwrap_or_default()
82 }
83
84 fn reliable_edges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)> {
85 self.reliable_edges.clone()
86 }
87}