zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};

use crate::{field_access, impl_default, impl_display};

/// Represents the channel folder's attributes.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChannelFolder {
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    order: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    date_created: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    creator_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    rendered_description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    is_archived: Option<bool>,
}

impl ChannelFolder {
    /// Creates a new [ChannelFolder].
    pub const fn new() -> Self {
        Self {
            id: None,
            name: None,
            order: None,
            date_created: None,
            creator_id: None,
            description: None,
            rendered_description: None,
            is_archived: None,
        }
    }
}

field_access! {
    ChannelFolder {
        /// The unique ID of the channel folder.
        id: option { u64 },
        /// This value determines in which order the channel folder should be displayed in the UI.
        ///
        /// The value is 0 indexed, and a channel folder with a lower value should be displayed before channel folders with higher values.
        ///
        /// **Changes**: New in Zulip 11.0 (feature level 414).
        order: option { u64 },
        /// The UNIX timestamp for when the channel folder was created, in UTC seconds.
        date_created: option { u64 },
        /// The ID of the user who created the channel folder.
        creator_id: option { u64 },
        /// Whether the channel folder is archived or not.
        is_archived: option { bool },
    }
}

field_access! {
    ChannelFolder {
        /// The name of the channel folder.
        name: option_cow_deref { str, String },
        /// The description of the channel folder. Can be an empty string.
        ///
        /// See [Markdown message formatting](https://zulip.com/api/message-formatting) for details on Zulip's HTML format.
        description: option_cow_deref { str, String },
        /// The description of the channel folder rendered as HTML, intended to be used for UI that displays the channel folder description.
        ///
        /// Clients should use the standard Zulip rendered_markdown CSS when displaying this content so that emoji, LaTeX, and other syntax work correctly.
        /// 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.
        rendered_description: option_cow_deref { str, String },
    }
}

impl_default!(ChannelFolder);
impl_display!(ChannelFolder);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_channel_folder() {
        let id = 1;
        let name = "test_name";
        let order = 2;
        let date_created = 3;
        let creator_id = 4;
        let description = "test_description";
        let rendered_description = "<p>test_description</p>";
        let is_archived = true;

        let channel_folder = ChannelFolder::new()
            .with_id(id)
            .with_name(name)
            .with_order(order)
            .with_date_created(date_created)
            .with_creator_id(creator_id)
            .with_description(description)
            .with_rendered_description(rendered_description)
            .with_is_archived(is_archived);

        let channel_folder_json = channel_folder.to_string();

        assert_eq!(
            serde_json::to_string(&channel_folder).unwrap(),
            channel_folder_json
        );
        assert_eq!(
            serde_json::from_str::<ChannelFolder>(&channel_folder_json).unwrap(),
            channel_folder
        );

        assert_eq!(channel_folder.id(), Some(id));
        assert_eq!(channel_folder.name(), Some(name));
        assert_eq!(channel_folder.order(), Some(order));
        assert_eq!(channel_folder.date_created(), Some(date_created));
        assert_eq!(channel_folder.creator_id(), Some(creator_id));
        assert_eq!(channel_folder.description(), Some(description));
        assert_eq!(
            channel_folder.rendered_description(),
            Some(rendered_description)
        );
        assert_eq!(channel_folder.is_archived(), Some(is_archived));
    }
}