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
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct MsgId(
#[cfg(not(feature = "long-message-id"))] pub i32,
#[cfg(feature = "long-message-id")] pub i64,
);
use crate::error::ProtocolError;
use crate::{deserialize::*, serialize::*};
use crate::serialize::UserType;
impl Serialize for MsgId {
fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
let mut res = Vec::new();
res.append(&mut Self::NAME.serialize_utf8()?);
res.extend(self.0.serialize()?);
Ok(res)
}
}
impl Deserialize for MsgId {
fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
#[cfg(not(feature = "long-message-id"))]
let (size, value) = i32::parse(b)?;
#[cfg(feature = "long-message-id")]
let (size, value) = i64::parse(b)?;
return Ok((size, MsgId(value)));
}
}
#[cfg(not(feature = "long-message-id"))]
impl From<i32> for MsgId {
fn from(value: i32) -> Self {
Self(value)
}
}
#[cfg(feature = "long-message-id")]
impl From<i64> for MsgId {
fn from(value: i64) -> Self {
Self(value)
}
}
impl std::ops::Deref for MsgId {
#[cfg(not(feature = "long-message-id"))]
type Target = i32;
#[cfg(feature = "long-message-id")]
type Target = i64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl UserType for MsgId {
const NAME: &str = "MsgId";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn msgid_parse_test() {
let test_bytes: &[u8] = if cfg!(feature = "long-message-id") {
&[0, 0, 0, 0, 0, 0, 0, 1]
} else {
&[0, 0, 0, 1]
};
let (len, res) = MsgId::parse(test_bytes).unwrap();
assert_eq!(len, test_bytes.len());
assert_eq!(res, MsgId(1));
}
#[test]
pub fn msgid_serialize_test() {
let res = MsgId(1).serialize().unwrap();
let expected_bytes: &[u8] = if cfg!(feature = "long-message-id") {
&[0, 0, 0, 5, 77, 115, 103, 73, 100, 0, 0, 0, 0, 0, 0, 0, 1]
} else {
&[0, 0, 0, 5, 77, 115, 103, 73, 100, 0, 0, 0, 1]
};
assert_eq!(res, expected_bytes);
}
}
|