1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use serde::{Deserialize, Serialize};
use crate::{field_access, impl_default, impl_display};
/// Represents a message draft.
///
/// 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 Draft {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<i64>,
#[serde(rename = "type")]
draft_type: DraftType,
to: Vec<u64>,
topic: String,
content: String,
#[serde(skip_serializing_if = "Option::is_none")]
timestamp: Option<u64>,
}
impl Draft {
/// Creates a new [Draft].
pub const fn new() -> Self {
Self {
id: None,
draft_type: DraftType::new(),
to: Vec::new(),
topic: String::new(),
content: String::new(),
timestamp: None,
}
}
}
field_access! {
Draft {
/// The unique ID of the draft.
///
/// It will only used whenever the drafts are fetched.
/// This field should not be specified when the draft is being created or edited.
id: option { i64 },
/// A Unix timestamp (seconds only) representing when the draft was last edited.
///
/// When creating a draft, this key need not be present and it will be filled in automatically by the server.
timestamp: option { u64 },
}
}
field_access! {
Draft {
/// The type of the draft.
///
/// Either unaddressed (empty string), `\"stream\"`, or `\"private\"` (for one-on-one and group direct messages).
draft_type: DraftType,
}
}
field_access! {
Draft {
/// An array of the tentative target audience IDs.
///
/// For channel messages, this should contain exactly 1 ID, the ID of the target channel.
/// For direct messages, this should be an array of target user IDs.
/// For unaddressed drafts, this is ignored, and clients should send an empty array.
to: cow { [u64] },
/// For channel message drafts, the tentative topic name.
///
/// For direct or unaddressed messages, this will be ignored and should ideally be the empty string.
/// Should not contain null bytes.
topic: cow { str },
/// The body of the draft.
///
/// Should not contain null bytes.
content: cow { str },
}
}
impl_default!(Draft);
impl_display!(Draft);
/// Represents the [Draft] type.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DraftType {
#[serde(rename = "")]
Unaddressed,
Stream,
Private,
}
impl DraftType {
/// Creates a new [DraftType].
pub const fn new() -> Self {
Self::Unaddressed
}
}
impl_default!(DraftType);
impl_display!(DraftType);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_draft() {
let id = 1;
let draft_type = DraftType::new();
let to = [2u64];
let topic = "test topic";
let content = "test content";
let timestamp = 3;
let draft = Draft::new()
.with_id(id)
.with_draft_type(draft_type)
.with_to(to.as_ref())
.with_topic(topic)
.with_content(content)
.with_timestamp(timestamp);
let draft_json = draft.to_string();
assert_eq!(serde_json::to_string(&draft).unwrap(), draft_json);
assert_eq!(serde_json::from_str::<Draft>(&draft_json).unwrap(), draft);
assert_eq!(draft.id(), Some(id));
assert_eq!(draft.draft_type(), draft_type);
assert_eq!(draft.to(), to.as_ref());
assert_eq!(draft.topic(), topic);
assert_eq!(draft.content(), content);
assert_eq!(draft.timestamp(), Some(timestamp));
}
}