Skip to main content

telltale_machine/
identity.rs

1//! Identity model for topology and capability assignment.
2
3use std::collections::{BTreeMap, BTreeSet};
4
5use serde::{Deserialize, Serialize};
6
7/// Participant identifier.
8#[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/// Site identifier.
18#[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
27/// Identity model for topology and capabilities.
28pub trait IdentityModel {
29    /// Participant type.
30    type ParticipantId: Clone + Ord;
31    /// Site type.
32    type SiteId: Clone + Ord;
33
34    /// Enumerate known sites.
35    fn sites(&self) -> Vec<Self::SiteId>;
36    /// Resolve a stable site display name.
37    fn site_name(&self, site: &Self::SiteId) -> String;
38    /// Capabilities provided by one site.
39    fn site_capabilities(&self, site: &Self::SiteId) -> BTreeSet<String>;
40    /// Set of reliable directed links.
41    fn reliable_edges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)>;
42}
43
44/// Simple static identity topology model.
45#[derive(Debug, Clone, Default, Serialize, Deserialize)]
46pub struct StaticIdentityModel {
47    /// Site metadata keyed by identifier.
48    pub sites: BTreeMap<SiteId, SiteInfo>,
49    /// Reliable directed links.
50    pub reliable_edges: BTreeSet<(SiteId, SiteId)>,
51}
52
53/// Site metadata.
54#[derive(Debug, Clone, Default, Serialize, Deserialize)]
55pub struct SiteInfo {
56    /// Display name for the site.
57    pub name: String,
58    /// Site capability labels.
59    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}