Skip to main content

coven_sync/
joiner.rs

1//! Who an invite's join request names, as the admin's app shows it (api.md E10).
2
3use coven_format::codes::InviteAdmits;
4
5/// Who asks to join through an invite (ยง13.2): one of two, as the invite
6/// admits. A member's own new device asks through an add-device pairing
7/// instead, which the format's request can say and an invite never takes.
8#[derive(Clone, Debug, PartialEq, Eq)]
9pub enum Joiner {
10    /// A new person, with the name they typed: the admin sees it with the
11    /// request, and the admission records it for the app's joining write.
12    NewPerson {
13        /// The name, which follows the `name` rule (format.md D2).
14        name: String,
15    },
16    /// An existing member getting new keys, whose profile already has a name.
17    MemberKeys,
18}
19
20impl Joiner {
21    /// What the invite this joiner answers admits.
22    pub fn admits(&self) -> InviteAdmits {
23        match self {
24            Self::NewPerson { .. } => InviteAdmits::NewPerson,
25            Self::MemberKeys => InviteAdmits::MemberKeys,
26        }
27    }
28}
29
30impl Joiner {
31    /// The format's joiner of a request that makes `circle`.
32    pub fn requesting(
33        self,
34        circle: coven_format::objects::JoiningCircle,
35    ) -> coven_format::objects::Joiner {
36        match self {
37            Self::NewPerson { name } => coven_format::objects::Joiner::NewPerson { name, circle },
38            Self::MemberKeys => coven_format::objects::Joiner::MemberKeys { circle },
39        }
40    }
41}
42
43/// A request for the member's own device is no invite's, and the app has no
44/// name for it.
45impl TryFrom<coven_format::objects::Joiner> for Joiner {
46    type Error = crate::SyncError;
47
48    fn try_from(joiner: coven_format::objects::Joiner) -> Result<Self, Self::Error> {
49        match joiner {
50            coven_format::objects::Joiner::NewPerson { name, .. } => Ok(Self::NewPerson { name }),
51            coven_format::objects::Joiner::MemberKeys { .. } => Ok(Self::MemberKeys),
52            coven_format::objects::Joiner::OwnDevice => {
53                Err(coven_format::Error::UnknownTag { field: "invite joiner", tag: 2 }.into())
54            }
55        }
56    }
57}
58
59#[cfg(test)]
60#[path = "joiner_tests.rs"]
61mod tests;