aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTobias Deiminger <tdmg@linutronix.de>2024-04-14 23:17:48 +0200
committerMax Audron <audron@cocaine.farm>2025-02-25 00:01:42 +0100
commit522258c809c9617bd6d92ee7305225a08803b369 (patch)
tree34eee000df8fc13607a09bedde95431c1770bf0e /src
parentadded session manager comments and log message (diff)
Add MsgId as Rust type
Up to now it was handled implicitely in Variant::UserType. Making it an explicit type allows to centralize the i32/i64 cfg dependency and to use the type for arguments in signalproxy::objects functions.
Diffstat (limited to 'src')
-rw-r--r--src/primitive/mod.rs2
-rw-r--r--src/primitive/msgid.rs54
2 files changed, 56 insertions, 0 deletions
diff --git a/src/primitive/mod.rs b/src/primitive/mod.rs
index 5124d3b..dbc4a58 100644
--- a/src/primitive/mod.rs
+++ b/src/primitive/mod.rs
@@ -1,6 +1,7 @@
mod bufferinfo;
mod datetime;
mod message;
+mod msgid;
mod signedint;
mod string;
mod stringlist;
@@ -12,6 +13,7 @@ mod variantmap;
pub use bufferinfo::*;
pub use datetime::*;
pub use message::*;
+pub use msgid::*;
#[allow(unused_imports)]
pub use signedint::*;
#[allow(unused_imports)]
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);
+ }
+}