Skip to main content

zulip_api/message/
display_recipient.rs

1use std::borrow::Cow;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{field_access, impl_cow, impl_default, impl_display, Error, Result};
6
7/// Represents the metadata for recipients who were displayed a message.
8#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
9#[serde(untagged)]
10pub enum DisplayRecipient {
11    /// Represents the channel for a message.
12    Channel(String),
13    /// Represents the list of user recipients of a message.
14    User(Vec<UserRecipient>),
15}
16
17impl DisplayRecipient {
18    /// Creates a new [DisplayRecipient].
19    pub const fn new() -> Self {
20        Self::Channel(String::new())
21    }
22
23    /// Gets the variant name.
24    pub const fn variant_name(&self) -> &'static str {
25        match self {
26            Self::Channel(_) => "DisplayRecipient::Channel",
27            Self::User(_) => "DisplayRecipient::User",
28        }
29    }
30
31    /// Converta a `String`-like argument into a [Self].
32    pub fn channel<'c, C: Into<Cow<'c, str>>>(val: C) -> Self {
33        Self::Channel(C::into(val).into())
34    }
35
36    /// Attempts to get the inner channel recipient.
37    pub fn as_channel(&self) -> Result<&str> {
38        match self {
39            Self::Channel(ch) => Ok(ch.as_str()),
40            _ => Err(Error::invalid_field(
41                "display_recipient",
42                self.variant_name(),
43                Self::Channel(Default::default()).variant_name(),
44            )),
45        }
46    }
47
48    /// Converta a `Vec<UserRecipient>`-like argument into a [Self].
49    pub fn user<'c, C: Into<Cow<'c, [UserRecipient]>>>(val: C) -> Self {
50        Self::User(C::into(val).into())
51    }
52
53    /// Attempts to get the inner user recipient.
54    pub fn as_user(&self) -> Result<&[UserRecipient]> {
55        match self {
56            Self::User(ch) => Ok(ch.as_slice()),
57            _ => Err(Error::invalid_field(
58                "display_recipient",
59                self.variant_name(),
60                Self::User(Default::default()).variant_name(),
61            )),
62        }
63    }
64}
65
66impl_cow!(DisplayRecipient);
67impl_default!(DisplayRecipient);
68impl_display!(DisplayRecipient);
69
70/// Represents metadata for a user who was displayed a message.
71#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
72pub struct UserRecipient {
73    id: u64,
74    email: String,
75    full_name: String,
76    is_mirror_dummy: bool,
77}
78
79impl UserRecipient {
80    /// Creates a new [UserRecipient].
81    pub const fn new() -> Self {
82        Self {
83            id: 0,
84            email: String::new(),
85            full_name: String::new(),
86            is_mirror_dummy: false,
87        }
88    }
89}
90
91field_access! {
92    UserRecipient {
93        id: u64,
94        is_mirror_dummy: bool,
95    }
96}
97
98field_access! {
99    UserRecipient {
100        email: cow { str },
101        full_name: cow { str },
102    }
103}
104
105impl_default!(UserRecipient);
106impl_display!(UserRecipient);
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_display_recipient() {
114        let id = 1;
115        let email = "user@example.com";
116        let full_name = "test user";
117        let is_mirror_dummy = true;
118
119        let user_recipient = UserRecipient::new()
120            .with_id(id)
121            .with_email(email)
122            .with_full_name(full_name)
123            .with_is_mirror_dummy(is_mirror_dummy);
124
125        let user_recipient_json = user_recipient.to_string();
126
127        assert_eq!(user_recipient.id(), id);
128        assert_eq!(user_recipient.email(), email);
129        assert_eq!(user_recipient.full_name(), full_name);
130        assert_eq!(user_recipient.is_mirror_dummy(), is_mirror_dummy);
131
132        assert_eq!(
133            serde_json::to_string(&user_recipient).unwrap(),
134            user_recipient_json
135        );
136        assert_eq!(
137            serde_json::from_str::<UserRecipient>(&user_recipient_json).unwrap(),
138            user_recipient
139        );
140
141        let channel = "test_channel";
142
143        let display_channel = DisplayRecipient::channel(channel);
144        assert_eq!(display_channel.as_channel(), Ok(channel));
145        assert!(display_channel.as_user().is_err());
146
147        let user_recipient_list = vec![user_recipient.clone()];
148        let display_user = DisplayRecipient::user(&user_recipient_list);
149        assert_eq!(display_user.as_user(), Ok(user_recipient_list.as_ref()));
150        assert!(display_user.as_channel().is_err());
151
152        [display_channel, display_user]
153            .into_iter()
154            .for_each(|display| {
155                let display_json = display.to_string();
156
157                assert_eq!(serde_json::to_string(&display).unwrap(), display_json);
158                assert_eq!(
159                    serde_json::from_str::<DisplayRecipient>(&display_json).unwrap(),
160                    display
161                );
162            });
163    }
164
165    #[test]
166    fn test_user_recipient() {
167        let id = 1;
168        let email = "user@example.com";
169        let full_name = "test user";
170        let is_mirror_dummy = true;
171
172        let user_recipient = UserRecipient::new()
173            .with_id(id)
174            .with_email(email)
175            .with_full_name(full_name)
176            .with_is_mirror_dummy(is_mirror_dummy);
177
178        let user_recipient_json = user_recipient.to_string();
179
180        assert_eq!(user_recipient.id(), id);
181        assert_eq!(user_recipient.email(), email);
182        assert_eq!(user_recipient.full_name(), full_name);
183        assert_eq!(user_recipient.is_mirror_dummy(), is_mirror_dummy);
184
185        assert_eq!(
186            serde_json::to_string(&user_recipient).unwrap(),
187            user_recipient_json
188        );
189        assert_eq!(
190            serde_json::from_str::<UserRecipient>(&user_recipient_json).unwrap(),
191            user_recipient
192        );
193    }
194}