aboutsummaryrefslogtreecommitdiff
path: root/src/primitive/peerptr.rs
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2025-02-26 17:53:08 +0100
committerMax Audron <audron@cocaine.farm>2025-02-26 17:53:08 +0100
commit6f9c0d0f2906d05e27f9f11af6cefdf006c2cf4b (patch)
tree3c92d681f8c98786bf7371ecfece60ed4f8d4a7c /src/primitive/peerptr.rs
parentadd MsgId and BufferId to objects where needed (diff)
refactor variant serialization code
Factored out a lot of the serialization of variants into trait's that have auto impl so code duplication is much reduced
Diffstat (limited to 'src/primitive/peerptr.rs')
-rw-r--r--src/primitive/peerptr.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/primitive/peerptr.rs b/src/primitive/peerptr.rs
new file mode 100644
index 0000000..f2a3e05
--- /dev/null
+++ b/src/primitive/peerptr.rs
@@ -0,0 +1,56 @@
+use crate::{
+ deserialize::Deserialize,
+ serialize::{Serialize, SerializeUTF8},
+};
+
+use crate::serialize::UserType;
+
+#[derive(Copy, Clone, Debug, std::cmp::PartialEq)]
+#[repr(transparent)]
+pub struct PeerPtr(pub i64);
+
+impl Serialize for PeerPtr {
+ fn serialize(&self) -> Result<Vec<u8>, crate::error::ProtocolError> {
+ let mut res = Vec::new();
+
+ res.append(&mut Self::NAME.serialize_utf8()?);
+ res.extend(self.0.serialize()?);
+
+ Ok(res)
+ }
+}
+
+impl Deserialize for PeerPtr {
+ fn parse(b: &[u8]) -> Result<(usize, Self), crate::error::ProtocolError> {
+ let (size, value) = i64::parse(b)?;
+ return Ok((size, PeerPtr(value)));
+ }
+}
+
+impl UserType for PeerPtr {
+ const NAME: &str = "PeerPtr";
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ // #[test]
+ // pub fn peerptr_parse_test() {
+ // let test_bytes: &[u8] = &[
+ // 0, 0, 0, 7, 80, 101, 101, 114, 80, 116, 114, 0, 0, 0, 0, 0, 0, 0, 1,
+ // ];
+ // let (len, res) = PeerPtr::parse(test_bytes).unwrap();
+ // assert_eq!(len, test_bytes.len());
+ // assert_eq!(res, PeerPtr(1));
+ // }
+
+ #[test]
+ pub fn peerptr_serialize_test() {
+ let res = PeerPtr(1).serialize().unwrap();
+ let expected_bytes: &[u8] = &[
+ 0, 0, 0, 7, 80, 101, 101, 114, 80, 116, 114, 0, 0, 0, 0, 0, 0, 0, 1,
+ ];
+ assert_eq!(res, expected_bytes);
+ }
+}