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, ReportType};

/// Represents the request for the `Report Message` Zulip API endpoint.
///
/// See [report-message](https://zulip.com/api/report-message) for details.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Request {
    #[serde(skip)]
    message_id: u64,
    report_type: ReportType,
    description: Option<String>,
}

impl Request {
    /// Creates a new [Request].
    pub const fn new() -> Self {
        Self {
            message_id: 0,
            report_type: ReportType::new(),
            description: None,
        }
    }
}

field_access! {
    Request {
        /// The target message's ID.
        message_id: u64,
        /// The reason that best describes why the current user is reporting the target message for moderation.
        report_type: ReportType,
    }
}

field_access! {
    Request {
        /// A short description with additional context about why the current user is reporting the target message for moderation.
        ///
        /// Clients should limit this string to a maximum length of 1000 characters.
        ///
        /// If the `report_type` parameter is ["other"](ReportType::Other), this parameter is required, and its value cannot be an empty string.
        description: option_cow_deref { str, String },
    }
}

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

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

    #[test]
    fn test_request() {
        let message_id = 1;
        let report_type = ReportType::new();
        let description = "test description";

        let mut request = Request::new()
            .with_message_id(message_id)
            .with_report_type(report_type)
            .with_description(description);
        let request_json = request.to_string();

        assert_eq!(request.message_id(), message_id);
        assert_eq!(request.report_type(), report_type);
        assert_eq!(request.description(), Some(description));

        assert_eq!(serde_json::to_string(&request).unwrap(), request_json);
        request.set_message_id(0);
        assert_eq!(
            serde_json::from_str::<Request>(&request_json).unwrap(),
            request
        );
    }
}