zulip_api/message/
edit_history.rs1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_cow, impl_default, impl_display};
4
5#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
7pub struct EditHistory {
8 #[serde(skip_serializing_if = "Option::is_none")]
9 prev_content: Option<String>,
10 #[serde(skip_serializing_if = "Option::is_none")]
11 prev_rendered_content: Option<String>,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 prev_stream: Option<u64>,
14 #[serde(skip_serializing_if = "Option::is_none")]
15 prev_topic: Option<String>,
16 #[serde(skip_serializing_if = "Option::is_none")]
17 stream: Option<String>,
18 #[serde(skip_serializing_if = "Option::is_none")]
19 timestamp: Option<u64>,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 topic: Option<String>,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 user_id: Option<u64>,
24}
25
26impl EditHistory {
27 pub const fn new() -> Self {
29 Self {
30 prev_content: None,
31 prev_rendered_content: None,
32 prev_stream: None,
33 prev_topic: None,
34 stream: None,
35 timestamp: None,
36 topic: None,
37 user_id: None,
38 }
39 }
40}
41
42field_access! {
43 EditHistory {
44 prev_content: option_cow_deref { str, String },
48 prev_rendered_content: option_cow_deref { str, String },
54 prev_topic: option_cow_deref { str, String },
63 stream: option_cow_deref { str, String },
70 topic: option_cow_deref { str, String },
76 }
77}
78
79field_access! {
80 EditHistory {
81 prev_stream: option { u64 },
87 timestamp: option { u64 },
89 user_id: option { u64 },
97 }
98}
99
100impl_cow!(EditHistory);
101impl_default!(EditHistory);
102impl_display!(EditHistory);
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_edit_history_minimal() {
110 let user_id = 42;
111 let timestamp = 468460800;
112
113 let edit_history = EditHistory::new()
114 .with_user_id(user_id)
115 .with_timestamp(timestamp);
116
117 let edit_history_json = edit_history.to_string();
118
119 assert_eq!(edit_history.timestamp(), Some(timestamp));
120 assert_eq!(edit_history.user_id(), Some(user_id));
121
122 assert_eq!(
123 serde_json::to_string(&edit_history).unwrap(),
124 edit_history_json
125 );
126 assert_eq!(
127 serde_json::from_str::<EditHistory>(&edit_history_json).unwrap(),
128 edit_history
129 );
130 }
131
132 #[test]
133 fn test_edit_history_full() {
134 let user_id = 42;
135 let timestamp = 468460800;
136 let prev_content = "prev content";
137 let prev_rendered_content = "prev rendered content";
138 let prev_stream = 1;
139 let prev_topic = "prev topic";
140 let stream = "stream";
141 let topic = "topic";
142
143 let edit_history = EditHistory::new()
144 .with_user_id(user_id)
145 .with_timestamp(timestamp)
146 .with_prev_content(prev_content)
147 .with_prev_rendered_content(prev_rendered_content)
148 .with_prev_stream(prev_stream)
149 .with_prev_topic(prev_topic)
150 .with_stream(stream)
151 .with_topic(topic);
152
153 let edit_history_json = edit_history.to_string();
154
155 assert_eq!(edit_history.timestamp(), Some(timestamp));
156 assert_eq!(edit_history.user_id(), Some(user_id));
157 assert_eq!(edit_history.prev_content(), Some(prev_content));
158 assert_eq!(
159 edit_history.prev_rendered_content(),
160 Some(prev_rendered_content)
161 );
162 assert_eq!(edit_history.prev_stream(), Some(prev_stream));
163 assert_eq!(edit_history.prev_topic(), Some(prev_topic));
164 assert_eq!(edit_history.stream(), Some(stream));
165 assert_eq!(edit_history.topic(), Some(topic));
166
167 assert_eq!(
168 serde_json::to_string(&edit_history).unwrap(),
169 edit_history_json
170 );
171 assert_eq!(
172 serde_json::from_str::<EditHistory>(&edit_history_json).unwrap(),
173 edit_history
174 );
175 }
176}