zulip-api 0.0.8

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

mod reaction_type;

pub use reaction_type::ReactionType;

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

/// Represents a reaction to a Zulip message.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Reaction {
    emoji_name: String,
    emoji_code: String,
    reaction_type: ReactionType,
    user_id: u64,
}

impl Reaction {
    /// Creates a new [Reaction].
    pub const fn new() -> Self {
        Self {
            emoji_name: String::new(),
            emoji_code: String::new(),
            reaction_type: ReactionType::new(),
            user_id: 0,
        }
    }
}

field_access! {
    Reaction {
        emoji_name: cow { str },
        emoji_code: cow { str },
    }
}

field_access! {
    Reaction {
        reaction_type: ReactionType,
        user_id: u64,
    }
}

impl_default!(Reaction);
impl_display!(Reaction);

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

    #[test]
    fn test_reaction_type() {
        [
            ReactionType::UnicodeEmoji,
            ReactionType::RealmEmoji,
            ReactionType::ZulipExtraEmoji,
        ]
        .into_iter()
        .for_each(|react_ty| {
            let react_json = react_ty.to_string();

            assert_eq!(serde_json::to_string(&react_ty).unwrap(), react_json);
            assert_eq!(
                serde_json::from_str::<ReactionType>(&react_json).unwrap(),
                react_ty
            );
        });
    }

    #[test]
    fn test_reaction() {
        [
            ReactionType::UnicodeEmoji,
            ReactionType::RealmEmoji,
            ReactionType::ZulipExtraEmoji,
        ]
        .into_iter()
        .for_each(|react_ty| {
            let emoji_name = "test emoji name";
            let emoji_code = "test emoji code";
            let user_id = 42;

            let react = Reaction::new()
                .with_emoji_name(emoji_name)
                .with_emoji_code(emoji_code)
                .with_reaction_type(react_ty)
                .with_user_id(user_id);

            let react_json = react.to_string();

            assert_eq!(serde_json::to_string(&react).unwrap(), react_json);
            assert_eq!(
                serde_json::from_str::<Reaction>(&react_json).unwrap(),
                react
            );

            assert_eq!(react.emoji_name(), emoji_name);
            assert_eq!(react.emoji_code(), emoji_code);
            assert_eq!(react.reaction_type(), react_ty);
            assert_eq!(react.user_id(), user_id);
        });
    }
}