zulip-api 0.0.8

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

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

/// Represents data about what value the user filled in the custom profile field with that ID.
///
/// Modified from `openapi-generator-cli` output:
///
/// ```shell,no_build,no_run
/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
/// ```
///
/// ```text
/// /*
/// * Zulip REST API
/// *
/// * Powerful open source group chat
/// *
/// * The version of the OpenAPI document: 1.0.0
/// *
/// * Generated by: https://openapi-generator.tech
/// */
/// ```
///
/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
///
/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ProfileData {
    #[serde(skip_serializing_if = "Option::is_none")]
    value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    rendered_value: Option<String>,
}

impl ProfileData {
    /// Creates a new [ProfileData].
    pub const fn new() -> ProfileData {
        ProfileData {
            value: None,
            rendered_value: None,
        }
    }
}

field_access! {
    ProfileData {
        /// User's personal value for this custom profile field.
        value: option_cow_deref { str, String },
        /// The `value` rendered in HTML.
        ///
        /// Will only be present for custom profile field types that support Markdown rendering.
        ///
        /// This user-generated HTML content should be rendered using the same CSS and client-side security protections as are used for message content.
        ///
        /// See [Markdown message formatting](https://zulip.com/api/message-formatting) for details on Zulip's HTML format.
        rendered_value: option_cow_deref { str, String },
    }
}

impl_cow!(ProfileData);
impl_default!(ProfileData);
impl_display!(ProfileData);

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

    #[test]
    fn test_profile_data() {
        let value = "test value";
        let rendered_value = "<p>test value</p>";

        let data = ProfileData::new()
            .with_value(value)
            .with_rendered_value(rendered_value);
        let data_json = data.to_string();

        assert_eq!(serde_json::to_string(&data).unwrap(), data_json);
        assert_eq!(
            serde_json::from_str::<ProfileData>(&data_json).unwrap(),
            data
        );

        assert_eq!(data.value(), Some(value));
        assert_eq!(data.rendered_value(), Some(rendered_value));
    }
}