Skip to main content

telltale_vm/
identity.rs

1//! Identity model aligned with Lean VM domain interfaces.
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/// Lean-style 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    /// Lean-name compatibility wrapper.
44    #[allow(non_snake_case)]
45    fn siteName(&self, site: &Self::SiteId) -> String {
46        self.site_name(site)
47    }
48
49    /// Lean-name compatibility wrapper.
50    #[allow(non_snake_case)]
51    fn siteCapabilities(&self, site: &Self::SiteId) -> BTreeSet<String> {
52        self.site_capabilities(site)
53    }
54
55    /// Lean-name compatibility wrapper.
56    #[allow(non_snake_case)]
57    fn reliableEdges(&self) -> BTreeSet<(Self::SiteId, Self::SiteId)> {
58        self.reliable_edges()
59    }
60}
61
62/// Simple static identity topology model.
63#[derive(Debug, Clone, Default, Serialize, Deserialize)]
64pub struct StaticIdentityModel {
65    /// Site metadata keyed by identifier.
66    pub sites: BTreeMap<SiteId, SiteInfo>,
67    /// Reliable directed links.
68    pub reliable_edges: BTreeSet<(SiteId, SiteId)>,
69}
70
71/// Site metadata.
72#[derive(Debug, Clone, Default, Serialize, Deserialize)]
73pub struct SiteInfo {
74    /// Display name for the site.
75    pub name: String,
76    /// Site capability labels.
77    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}