crabka_replicator/mm2/
offset_sync.rs1use crate::error::ReplicatorError;
2
3use super::{Reader, Writer};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct OffsetSync {
7 pub topic: String,
8 pub partition: i32,
9 pub upstream: i64,
10 pub downstream: i64,
11}
12
13impl OffsetSync {
14 #[must_use]
15 pub fn topic_name(source_alias: &str) -> String {
16 format!("mm2-offset-syncs.{source_alias}.internal")
17 }
18
19 #[must_use]
20 pub fn key_bytes(&self) -> Vec<u8> {
21 Writer::keyless()
22 .string(&self.topic)
23 .i32(self.partition)
24 .finish()
25 }
26
27 #[must_use]
28 pub fn value_bytes(&self) -> Vec<u8> {
29 Writer::keyless()
33 .i64(self.upstream)
34 .i64(self.downstream)
35 .finish()
36 }
37
38 pub fn from_bytes(key: &[u8], val: &[u8]) -> Result<Self, ReplicatorError> {
39 let mut k = Reader::keyless(key);
40 let mut v = Reader::keyless(val);
41 Ok(Self {
42 topic: k.string()?,
43 partition: k.i32()?,
44 upstream: v.i64()?,
45 downstream: v.i64()?,
46 })
47 }
48}