zulip-api 0.0.8

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

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

/// Represents a `Edit Message` request to a Zulip server.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
    #[serde(skip)]
    message_id: u64,
    content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    topic: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    propagate_mode: Option<PropagateMode>,
    #[serde(skip_serializing_if = "Option::is_none")]
    send_notification_to_old_thread: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    send_notification_to_new_thread: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    prev_content_sha256: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stream_id: Option<u64>,
}

impl Request {
    /// Creates a new [Request].
    pub const fn new() -> Self {
        Self {
            message_id: 0,
            content: String::new(),
            topic: None,
            propagate_mode: None,
            send_notification_to_old_thread: None,
            send_notification_to_new_thread: None,
            prev_content_sha256: None,
            stream_id: None,
        }
    }
}

field_access! {
    Request {
        message_id: u64,
    }
}

field_access! {
    Request {
        content: cow { str },
    }
}

field_access! {
    Request {
        topic: option_cow_deref { str, String },
        prev_content_sha256: option_cow_deref { str, String },
    }
}

field_access! {
    Request {
        propagate_mode: option { PropagateMode },
        send_notification_to_old_thread: option { bool },
        send_notification_to_new_thread: option { bool },
        stream_id: option { u64 },
    }
}

impl_default!(Request);
impl_display!(Request);

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

    #[test]
    fn test_edit_message_minimal() {
        let message_id = 1;

        let content = "content";
        let content_json = serde_json::to_string(&content).unwrap();

        let mut req = Request::new()
            .with_message_id(message_id)
            .with_content(content);

        let req_json = format!(r#"{{"content":{content_json}}}"#);

        assert_eq!(req.message_id(), message_id);
        assert_eq!(req.content(), content);

        assert_eq!(serde_json::to_string(&req).unwrap(), req_json);

        req.set_message_id(0);
        assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
    }

    #[test]
    fn test_edit_message_full() {
        let message_id = 1;

        let content = "content";
        let content_json = serde_json::to_string(&content).unwrap();

        let topic = "topic";
        let topic_json = serde_json::to_string(&topic).unwrap();

        let propagate_mode = PropagateMode::new();
        let propagate_mode_json = serde_json::to_string(&propagate_mode).unwrap();

        let send_notification_to_old_thread = false;
        let send_notification_to_old_thread_json =
            serde_json::to_string(&send_notification_to_old_thread).unwrap();

        let send_notification_to_new_thread = false;
        let send_notification_to_new_thread_json =
            serde_json::to_string(&send_notification_to_new_thread).unwrap();

        let prev_content_sha256 =
            "6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f76863140143ff72";
        let prev_content_sha256_json = serde_json::to_string(&prev_content_sha256).unwrap();

        let stream_id = 2;
        let stream_id_json = serde_json::to_string(&stream_id).unwrap();

        let mut req = Request::new()
            .with_message_id(message_id)
            .with_content(content)
            .with_topic(topic)
            .with_propagate_mode(propagate_mode)
            .with_send_notification_to_old_thread(send_notification_to_old_thread)
            .with_send_notification_to_new_thread(send_notification_to_new_thread)
            .with_prev_content_sha256(prev_content_sha256)
            .with_stream_id(stream_id);

        let req_json = format!(
            r#"{{"content":{content_json},"topic":{topic_json},"propagate_mode":{propagate_mode_json},"send_notification_to_old_thread":{send_notification_to_old_thread_json},"send_notification_to_new_thread":{send_notification_to_new_thread_json},"prev_content_sha256":{prev_content_sha256_json},"stream_id":{stream_id_json}}}"#
        );

        assert_eq!(req.message_id(), message_id);
        assert_eq!(req.content(), content);

        assert_eq!(req.propagate_mode(), Some(propagate_mode));
        assert_eq!(
            req.send_notification_to_old_thread(),
            Some(send_notification_to_old_thread)
        );
        assert_eq!(
            req.send_notification_to_new_thread(),
            Some(send_notification_to_new_thread)
        );
        assert_eq!(req.prev_content_sha256(), Some(prev_content_sha256));
        assert_eq!(req.stream_id(), Some(stream_id));

        assert_eq!(serde_json::to_string(&req).unwrap(), req_json);

        req.set_message_id(0);
        assert_eq!(serde_json::from_str::<Request>(&req_json).unwrap(), req);
    }
}