zulip_api/message/profile_data.rs
1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_cow, impl_default, impl_display};
4
5/// Represents data about what value the user filled in the custom profile field with that ID.
6///
7/// Modified from `openapi-generator-cli` output:
8///
9/// ```shell,no_build,no_run
10/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
11/// ```
12///
13/// ```text
14/// /*
15/// * Zulip REST API
16/// *
17/// * Powerful open source group chat
18/// *
19/// * The version of the OpenAPI document: 1.0.0
20/// *
21/// * Generated by: https://openapi-generator.tech
22/// */
23/// ```
24///
25/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
26///
27/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
28#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
29pub struct ProfileData {
30 #[serde(skip_serializing_if = "Option::is_none")]
31 value: Option<String>,
32 #[serde(skip_serializing_if = "Option::is_none")]
33 rendered_value: Option<String>,
34}
35
36impl ProfileData {
37 /// Creates a new [ProfileData].
38 pub const fn new() -> ProfileData {
39 ProfileData {
40 value: None,
41 rendered_value: None,
42 }
43 }
44}
45
46field_access! {
47 ProfileData {
48 /// User's personal value for this custom profile field.
49 value: option_cow_deref { str, String },
50 /// The `value` rendered in HTML.
51 ///
52 /// Will only be present for custom profile field types that support Markdown rendering.
53 ///
54 /// This user-generated HTML content should be rendered using the same CSS and client-side security protections as are used for message content.
55 ///
56 /// See [Markdown message formatting](https://zulip.com/api/message-formatting) for details on Zulip's HTML format.
57 rendered_value: option_cow_deref { str, String },
58 }
59}
60
61impl_cow!(ProfileData);
62impl_default!(ProfileData);
63impl_display!(ProfileData);
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn test_profile_data() {
71 let value = "test value";
72 let rendered_value = "<p>test value</p>";
73
74 let data = ProfileData::new()
75 .with_value(value)
76 .with_rendered_value(rendered_value);
77 let data_json = data.to_string();
78
79 assert_eq!(serde_json::to_string(&data).unwrap(), data_json);
80 assert_eq!(
81 serde_json::from_str::<ProfileData>(&data_json).unwrap(),
82 data
83 );
84
85 assert_eq!(data.value(), Some(value));
86 assert_eq!(data.rendered_value(), Some(rendered_value));
87 }
88}