Skip to main content

zaun_community_protocol/
model.rs

1//! Enumerations shared by permissions, wire bodies and the engine.
2//!
3//! JSON form (specification §7): `{"t":"<variant>"}` for unit variants and
4//! `{"t":"<variant>","c":<content>}` otherwise, snake_case names.
5
6use serde::{Deserialize, Serialize};
7
8/// A member's lifecycle state (specification §6.4, ADR-0016). Independent of roles.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(tag = "t", content = "c", rename_all = "snake_case")]
11pub enum MembershipState {
12    /// Awaiting approval; may only read the community summary and leave.
13    Pending,
14    /// Admitted with the admission policy's newcomer restrictions.
15    Probation,
16    /// A full member.
17    Active,
18    /// Temporarily removed from participation; keeps the binding.
19    Suspended,
20    /// Banned; the binding is revoked.
21    Banned,
22    /// Left or kicked.
23    Left,
24}
25
26impl MembershipState {
27    /// Whether the member holds a live binding (may issue at least some commands).
28    #[must_use]
29    pub const fn is_bound(self) -> bool {
30        matches!(
31            self,
32            Self::Pending | Self::Probation | Self::Active | Self::Suspended
33        )
34    }
35
36    /// Whether the member counts as admitted (receives channel events if permitted).
37    #[must_use]
38    pub const fn is_admitted(self) -> bool {
39        matches!(self, Self::Probation | Self::Active)
40    }
41}
42
43/// How much channel history a member may read (ADR-0014, specification §8.2, §10.6).
44/// Stamped on each message at send time.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
46#[serde(tag = "t", content = "c", rename_all = "snake_case")]
47pub enum HistoryAccess {
48    /// Only messages sent while the member can see the channel live.
49    None,
50    /// Messages sent after the member gained access.
51    SinceJoined,
52    /// The last `n` messages before the member gained access (1 ≤ n ≤ 10 000), then live.
53    LastN(u32),
54    /// Everything.
55    #[default]
56    Full,
57}
58
59/// How accepted channel-stream events travel (ADR-0009, specification §9.1, Appendix A).
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
61#[serde(tag = "t", content = "c", rename_all = "snake_case")]
62pub enum DistributionMode {
63    /// One SimpleX message per recipient over each member's control connection (v1).
64    #[default]
65    DirectSimplex,
66    /// A native SimpleX public channel via chat relays. Experimental (ADR-0010).
67    NativePublicChannel,
68}
69
70/// The kind of a channel (permissions §1.3, specification §8.2).
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
72#[serde(tag = "t", content = "c", rename_all = "snake_case")]
73pub enum ChannelKind {
74    /// Groups channels; its overrides apply to synced children.
75    Category,
76    /// Text chat.
77    Text,
78    /// Text chat where `@everyone` cannot post by default.
79    Announcement,
80    /// Reserved: forum posts (future).
81    Forum,
82    /// Reserved: voice (future); has text chat.
83    Voice,
84    /// Reserved: stage (future); has text chat.
85    Stage,
86    /// A direct-message channel between one bot and one member (§8.15); only its two
87    /// participants can see it, whatever their roles.
88    Dm,
89}
90
91/// Who sees that someone is typing in a channel (`typing`, specification §8.2, §10.5).
92/// A plain JSON string, as the specification writes it.
93#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
94#[serde(rename_all = "snake_case")]
95pub enum TypingMode {
96    /// `signal.typing` names the members typing (the default).
97    #[default]
98    Named,
99    /// `signal.typing` carries only how many are typing (1, 2, or 3 for "several"), never who.
100    Anonymous,
101    /// The host drops typing signals in the channel; clients do not send them.
102    Off,
103}
104
105impl TypingMode {
106    /// Whether this is the default, [`TypingMode::Named`].
107    #[must_use]
108    pub const fn is_named(&self) -> bool {
109        matches!(self, Self::Named)
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn json_forms_match_the_specification() {
119        let cases = [
120            (
121                serde_json::to_string(&HistoryAccess::None).unwrap(),
122                r#"{"t":"none"}"#,
123            ),
124            (
125                serde_json::to_string(&HistoryAccess::SinceJoined).unwrap(),
126                r#"{"t":"since_joined"}"#,
127            ),
128            (
129                serde_json::to_string(&HistoryAccess::LastN(50)).unwrap(),
130                r#"{"t":"last_n","c":50}"#,
131            ),
132            (
133                serde_json::to_string(&DistributionMode::DirectSimplex).unwrap(),
134                r#"{"t":"direct_simplex"}"#,
135            ),
136            (
137                serde_json::to_string(&MembershipState::Probation).unwrap(),
138                r#"{"t":"probation"}"#,
139            ),
140            (
141                serde_json::to_string(&ChannelKind::Announcement).unwrap(),
142                r#"{"t":"announcement"}"#,
143            ),
144        ];
145        for (got, want) in cases {
146            assert_eq!(got, want);
147        }
148        assert_eq!(
149            serde_json::from_str::<HistoryAccess>(r#"{"t":"last_n","c":7}"#).unwrap(),
150            HistoryAccess::LastN(7)
151        );
152        assert!(serde_json::from_str::<MembershipState>(r#"{"t":"owner"}"#).is_err());
153        assert_eq!(
154            serde_json::to_string(&TypingMode::Anonymous).unwrap(),
155            r#""anonymous""#
156        );
157        assert!(serde_json::from_str::<TypingMode>(r#""secret""#).is_err());
158    }
159}