diff options
Diffstat (limited to 'src/primitive/msgid.rs')
| -rw-r--r-- | src/primitive/msgid.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/primitive/msgid.rs b/src/primitive/msgid.rs new file mode 100644 index 0000000..509c1e7 --- /dev/null +++ b/src/primitive/msgid.rs @@ -0,0 +1,54 @@ +#[derive(Copy, Clone, Debug, std::cmp::PartialEq)] +pub struct MsgId( + #[cfg(not(feature = "long-message-id"))] pub i32, + #[cfg(feature = "long-message-id")] pub i64, +); + +use failure::Error; + +use crate::primitive::signedint; +use crate::{deserialize::*, serialize::*}; + +impl Serialize for MsgId { + fn serialize(&self) -> Result<Vec<u8>, Error> { + self.0.serialize() + } +} + +impl Deserialize for MsgId { + fn parse(b: &[u8]) -> Result<(usize, Self), Error> { + #[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(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, 0, 0, 0, 0, 1] + } else { + &[0, 0, 0, 1] + }; + assert_eq!(res, expected_bytes); + } +} |
