-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtyping_indicator.rs
More file actions
231 lines (199 loc) · 6.91 KB
/
typing_indicator.rs
File metadata and controls
231 lines (199 loc) · 6.91 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Typing indicator message related types.
use thiserror::Error;
/// A typing indicator status indicates when a contact is typing or stopped typing.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TypingStatus {
/// The contact is not typing
NotTyping,
/// The contact is currently typing
Typing,
}
impl TypingStatus {
/// Return whether or not the user is typing.
#[must_use]
pub fn is_typing(self) -> bool {
self == TypingStatus::Typing
}
}
impl From<TypingStatus> for u8 {
fn from(val: TypingStatus) -> Self {
match val {
TypingStatus::NotTyping => 0x00,
TypingStatus::Typing => 0x01,
}
}
}
impl From<TypingStatus> for bool {
fn from(val: TypingStatus) -> Self {
val.is_typing()
}
}
impl From<bool> for TypingStatus {
fn from(val: bool) -> Self {
if val {
TypingStatus::Typing
} else {
TypingStatus::NotTyping
}
}
}
impl TryFrom<u8> for TypingStatus {
type Error = InvalidTypingIndicatorValue;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0x00 => Ok(TypingStatus::NotTyping),
0x01 => Ok(TypingStatus::Typing),
_ => Err(InvalidTypingIndicatorValue(value)),
}
}
}
/// Errors when parsing a typing indicator message.
#[derive(Debug, PartialEq, Clone, Error)]
pub enum TypingIndicatorMessageParseError {
/// Invalid message length (must be exactly 1 byte)
#[error("invalid message byte length (must be 1 byte): {0}")]
InvalidLength(usize),
/// Invalid value (must be 0 or 1)
#[error("invalid typing indicator value: {0}")]
InvalidValue(#[from] InvalidTypingIndicatorValue),
}
/// An invalid typing indicator value was encountered.
#[derive(Debug, PartialEq, Clone, Error)]
#[error("invalid typing indicator value: {0} (must be 0 or 1)")]
pub struct InvalidTypingIndicatorValue(pub u8);
/// A typing indicator message.
///
/// Contains a single byte indicating whether the contact is typing or not.
///
/// Note: While active, typing indicators should be re-sent every 10s. When no
/// typing indicator message is received for 15s, the clients will stop showing
/// the typing status.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TypingIndicatorMessage {
/// The typing indicator status
pub status: TypingStatus,
}
impl TypingIndicatorMessage {
/// Create a new [`TypingIndicatorMessage`].
#[must_use]
pub const fn new(indicator: TypingStatus) -> Self {
Self { status: indicator }
}
/// Encode this message to its wire-format bytes.
///
/// Returns a single byte: 1 if typing, 0 if stopped typing.
#[must_use]
pub fn encode(&self) -> Vec<u8> {
vec![u8::from(self.status)]
}
/// Decode a typing indicator message from raw bytes.
///
/// The first (and only) byte must be 0 (stopped typing) or 1 (typing).
pub fn decode(bytes: &[u8]) -> Result<Self, TypingIndicatorMessageParseError> {
match bytes {
&[byte] => Ok(Self {
status: TypingStatus::try_from(byte)?,
}),
_ => Err(TypingIndicatorMessageParseError::InvalidLength(bytes.len())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
mod typing_indicator {
use super::*;
#[test]
fn to_u8() {
assert_eq!(u8::from(TypingStatus::NotTyping), 0x00);
assert_eq!(u8::from(TypingStatus::Typing), 0x01);
}
#[test]
fn try_from_u8() {
assert_eq!(TypingStatus::try_from(0x00), Ok(TypingStatus::NotTyping));
assert_eq!(TypingStatus::try_from(0x01), Ok(TypingStatus::Typing));
assert_eq!(
TypingStatus::try_from(0x02),
Err(InvalidTypingIndicatorValue(0x02))
);
assert_eq!(
TypingStatus::try_from(0xff),
Err(InvalidTypingIndicatorValue(0xff))
);
}
#[test]
fn round_trip() {
let typing = TypingStatus::Typing;
assert_eq!(TypingStatus::try_from(u8::from(typing)).unwrap(), typing);
let stopped = TypingStatus::NotTyping;
assert_eq!(TypingStatus::try_from(u8::from(stopped)).unwrap(), stopped);
}
}
mod typing_indicator_message {
use super::*;
#[test]
fn new_typing() {
let msg = TypingIndicatorMessage::new(TypingStatus::Typing);
assert_eq!(msg.status, TypingStatus::Typing);
}
#[test]
fn new_stopped_typing() {
let msg = TypingIndicatorMessage::new(TypingStatus::NotTyping);
assert_eq!(msg.status, TypingStatus::NotTyping);
}
#[test]
fn encode_typing() {
let msg = TypingIndicatorMessage::new(TypingStatus::Typing);
let bytes = msg.encode();
assert_eq!(bytes, vec![0x01]);
}
#[test]
fn encode_stopped_typing() {
let msg = TypingIndicatorMessage::new(TypingStatus::NotTyping);
let bytes = msg.encode();
assert_eq!(bytes, vec![0x00]);
}
#[test]
fn decode_typing() {
let msg = TypingIndicatorMessage::decode(&[0x01]).expect("valid bytes should decode");
assert_eq!(msg.status, TypingStatus::Typing);
}
#[test]
fn decode_stopped_typing() {
let msg = TypingIndicatorMessage::decode(&[0x00]).expect("valid bytes should decode");
assert_eq!(msg.status, TypingStatus::NotTyping);
}
#[test]
fn decode_empty() {
let err = TypingIndicatorMessage::decode(&[]).unwrap_err();
assert_eq!(err, TypingIndicatorMessageParseError::InvalidLength(0));
}
#[test]
fn decode_too_long() {
let err = TypingIndicatorMessage::decode(&[0x01, 0x02]).unwrap_err();
assert_eq!(err, TypingIndicatorMessageParseError::InvalidLength(2));
}
#[test]
fn decode_invalid_value() {
let err = TypingIndicatorMessage::decode(&[0x02]).unwrap_err();
assert_eq!(
err,
TypingIndicatorMessageParseError::InvalidValue(InvalidTypingIndicatorValue(0x02))
);
}
#[test]
fn round_trip_typing() {
let original = TypingIndicatorMessage::new(TypingStatus::Typing);
let decoded = TypingIndicatorMessage::decode(&original.encode())
.expect("round-trip decode should succeed");
assert_eq!(decoded, original);
}
#[test]
fn round_trip_stopped_typing() {
let original = TypingIndicatorMessage::new(TypingStatus::NotTyping);
let decoded = TypingIndicatorMessage::decode(&original.encode())
.expect("round-trip decode should succeed");
assert_eq!(decoded, original);
}
}
}