Skip to main content

krabka_replicator/mm2/
heartbeat.rs

1use super::{Reader, Writer};
2use crate::error::ReplicatorError;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Heartbeat {
6    pub source: String,
7    pub target: String,
8    pub timestamp_ms: i64,
9}
10
11impl Heartbeat {
12    pub const TOPIC: &'static str = "heartbeats";
13
14    #[must_use]
15    pub fn key_bytes(&self) -> Vec<u8> {
16        Writer::keyless()
17            .string(&self.source)
18            .string(&self.target)
19            .finish()
20    }
21
22    #[must_use]
23    pub fn value_bytes(&self) -> Vec<u8> {
24        Writer::new().i64(self.timestamp_ms).finish()
25    }
26
27    /// # Errors
28    /// Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.
29    pub fn from_bytes(key: &[u8], val: &[u8]) -> Result<Self, ReplicatorError> {
30        let mut k = Reader::keyless(key);
31        let mut v = Reader::new(val)?;
32        Ok(Self {
33            source: k.string()?,
34            target: k.string()?,
35            timestamp_ms: v.i64()?,
36        })
37    }
38}