krabka_raft/kraft/transport/wire/
codec.rs1use {
11 crate::kraft::types::{Epoch, NodeId},
12 base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD},
13 bytes::{Bytes, BytesMut},
14 krabka_protocol::{Encode, primitives::uuid::Uuid as MetaUuid, records::RecordsPayload},
15};
16
17pub const METADATA_TOPIC: &str = "__cluster_metadata";
19pub const METADATA_PARTITION: i32 = 0;
21pub const METADATA_TOPIC_ID: MetaUuid = MetaUuid([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
24
25pub const VOTE_VERSION: i16 = 2;
27pub const QUORUM_EPOCH_VERSION: i16 = 1;
28pub const FETCH_VERSION: i16 = 18;
29pub const FETCH_SNAPSHOT_VERSION: i16 = 1;
30
31pub fn records_payload_to_bytes(payload: &RecordsPayload) -> Bytes {
32 match payload {
33 RecordsPayload::Raw(bytes) => bytes.clone(),
34 other => {
35 let mut out = BytesMut::new();
36 let _ = other.encode_to(&mut out);
37 out.freeze()
38 }
39 }
40}
41
42pub fn epoch_to_wire(e: Epoch) -> i32 { i32::try_from(e).unwrap_or(i32::MAX) }
47pub fn epoch_from_wire(e: i32) -> Epoch { u32::try_from(e).unwrap_or(0) }
48pub fn node_to_wire(n: NodeId) -> i32 { i32::try_from(n.0).unwrap_or(i32::MAX) }
50pub fn node_from_wire(n: i32) -> NodeId { NodeId(u64::try_from(n).unwrap_or(0)) }
51pub fn uuid_to_wire(id: uuid::Uuid) -> MetaUuid { MetaUuid(id.into_bytes()) }
53pub fn uuid_from_wire(id: MetaUuid) -> uuid::Uuid { uuid::Uuid::from_bytes(id.0) }
54pub fn cluster_id_to_wire(id: Option<uuid::Uuid>) -> Option<String> {
56 id.map(|id| URL_SAFE_NO_PAD.encode(id.as_bytes()))
57}
58
59pub fn encode_body<T: Encode>(msg: &T, version: i16) -> Bytes {
60 let mut buf = BytesMut::new();
61 let _ = msg.encode(&mut buf, version);
64 buf.freeze()
65}