aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Deiminger <tdmg@linutronix.de>2024-04-16 22:06:31 +0200
committerMax Audron <audron@cocaine.farm>2025-02-25 00:01:50 +0100
commit96536e2d0082915c0704731ed129e64f47b86ca5 (patch)
tree656b50bff2029a9fe6f416b68eda003f586a0ed1
parentUse MsgId in Message (diff)
Add BufferId as Rust type
Up to now it was represented as i32. If we introduce a newtype for it, we can handle it idiomatically as dedicated Variant::BufferId variant (instead of having it mashed into Variant::UserType).
-rw-r--r--src/primitive/bufferid.rs40
-rw-r--r--src/primitive/mod.rs2
2 files changed, 42 insertions, 0 deletions
diff --git a/src/primitive/bufferid.rs b/src/primitive/bufferid.rs
new file mode 100644
index 0000000..dc20001
--- /dev/null
+++ b/src/primitive/bufferid.rs
@@ -0,0 +1,40 @@
+#[derive(Copy, Clone, Debug, std::cmp::PartialEq)]
+pub struct BufferId(pub i32);
+
+use failure::Error;
+
+use crate::primitive::signedint;
+use crate::{deserialize::*, serialize::*};
+
+impl Serialize for BufferId {
+ fn serialize(&self) -> Result<Vec<u8>, Error> {
+ self.0.serialize()
+ }
+}
+
+impl Deserialize for BufferId {
+ fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
+ let (size, value) = i32::parse(b)?;
+ return Ok((size, BufferId(value)));
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ pub fn bufferid_parse_test() {
+ let test_bytes: &[u8] = &[0, 0, 0, 1];
+ let (len, res) = BufferId::parse(test_bytes).unwrap();
+ assert_eq!(len, test_bytes.len());
+ assert_eq!(res, BufferId(1));
+ }
+
+ #[test]
+ pub fn bufferid_serialize_test() {
+ let res = BufferId(1).serialize().unwrap();
+ let expected_bytes: &[u8] = &[0, 0, 0, 1];
+ assert_eq!(res, expected_bytes);
+ }
+}
diff --git a/src/primitive/mod.rs b/src/primitive/mod.rs
index dbc4a58..67bb2ac 100644
--- a/src/primitive/mod.rs
+++ b/src/primitive/mod.rs
@@ -1,3 +1,4 @@
+mod bufferid;
mod bufferinfo;
mod datetime;
mod message;
@@ -10,6 +11,7 @@ mod variant;
mod variantlist;
mod variantmap;
+pub use bufferid::*;
pub use bufferinfo::*;
pub use datetime::*;
pub use message::*;