Skip to main content

zulip_api/message/
channel_folder.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_default, impl_display};
4
5/// Represents the channel folder's attributes.
6#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
7pub struct ChannelFolder {
8    #[serde(skip_serializing_if = "Option::is_none")]
9    id: Option<u64>,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    name: Option<String>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    order: Option<u64>,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    date_created: Option<u64>,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    creator_id: Option<u64>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    description: Option<String>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    rendered_description: Option<String>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    is_archived: Option<bool>,
24}
25
26impl ChannelFolder {
27    /// Creates a new [ChannelFolder].
28    pub const fn new() -> Self {
29        Self {
30            id: None,
31            name: None,
32            order: None,
33            date_created: None,
34            creator_id: None,
35            description: None,
36            rendered_description: None,
37            is_archived: None,
38        }
39    }
40}
41
42field_access! {
43    ChannelFolder {
44        /// The unique ID of the channel folder.
45        id: option { u64 },
46        /// This value determines in which order the channel folder should be displayed in the UI.
47        ///
48        /// The value is 0 indexed, and a channel folder with a lower value should be displayed before channel folders with higher values.
49        ///
50        /// **Changes**: New in Zulip 11.0 (feature level 414).
51        order: option { u64 },
52        /// The UNIX timestamp for when the channel folder was created, in UTC seconds.
53        date_created: option { u64 },
54        /// The ID of the user who created the channel folder.
55        creator_id: option { u64 },
56        /// Whether the channel folder is archived or not.
57        is_archived: option { bool },
58    }
59}
60
61field_access! {
62    ChannelFolder {
63        /// The name of the channel folder.
64        name: option_cow_deref { str, String },
65        /// The description of the channel folder. Can be an empty string.
66        ///
67        /// See [Markdown message formatting](https://zulip.com/api/message-formatting) for details on Zulip's HTML format.
68        description: option_cow_deref { str, String },
69        /// The description of the channel folder rendered as HTML, intended to be used for UI that displays the channel folder description.
70        ///
71        /// Clients should use the standard Zulip rendered_markdown CSS when displaying this content so that emoji, LaTeX, and other syntax work correctly.
72        /// And any client-side security logic for user-generated message content should be applied when displaying this HTML as though it were the body of a Zulip message.
73        rendered_description: option_cow_deref { str, String },
74    }
75}
76
77impl_default!(ChannelFolder);
78impl_display!(ChannelFolder);
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_channel_folder() {
86        let id = 1;
87        let name = "test_name";
88        let order = 2;
89        let date_created = 3;
90        let creator_id = 4;
91        let description = "test_description";
92        let rendered_description = "<p>test_description</p>";
93        let is_archived = true;
94
95        let channel_folder = ChannelFolder::new()
96            .with_id(id)
97            .with_name(name)
98            .with_order(order)
99            .with_date_created(date_created)
100            .with_creator_id(creator_id)
101            .with_description(description)
102            .with_rendered_description(rendered_description)
103            .with_is_archived(is_archived);
104
105        let channel_folder_json = channel_folder.to_string();
106
107        assert_eq!(
108            serde_json::to_string(&channel_folder).unwrap(),
109            channel_folder_json
110        );
111        assert_eq!(
112            serde_json::from_str::<ChannelFolder>(&channel_folder_json).unwrap(),
113            channel_folder
114        );
115
116        assert_eq!(channel_folder.id(), Some(id));
117        assert_eq!(channel_folder.name(), Some(name));
118        assert_eq!(channel_folder.order(), Some(order));
119        assert_eq!(channel_folder.date_created(), Some(date_created));
120        assert_eq!(channel_folder.creator_id(), Some(creator_id));
121        assert_eq!(channel_folder.description(), Some(description));
122        assert_eq!(
123            channel_folder.rendered_description(),
124            Some(rendered_description)
125        );
126        assert_eq!(channel_folder.is_archived(), Some(is_archived));
127    }
128}