use crate::WireError;
use crate::codec::{WireDecode, WireEncode, read_u32_le, read_u64_le};
use bytes::{BufMut, BytesMut};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CreatedPartitionAssignment {
pub partition_id: u32,
pub consensus_group_id: u64,
}
impl CreatedPartitionAssignment {
pub const ENCODED_SIZE: usize = 12;
}
impl WireEncode for CreatedPartitionAssignment {
fn encoded_size(&self) -> usize {
Self::ENCODED_SIZE
}
fn encode(&self, buf: &mut BytesMut) {
buf.put_u32_le(self.partition_id);
buf.put_u64_le(self.consensus_group_id);
}
}
impl WireDecode for CreatedPartitionAssignment {
fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
if buf.len() < Self::ENCODED_SIZE {
return Err(WireError::UnexpectedEof {
offset: 0,
need: Self::ENCODED_SIZE,
have: buf.len(),
});
}
Ok((
Self {
partition_id: read_u32_le(buf, 0)?,
consensus_group_id: read_u64_le(buf, 4)?,
},
12,
))
}
}
#[cfg(test)]
mod tests {
use super::CreatedPartitionAssignment;
use crate::codec::{WireDecode, WireEncode};
#[test]
fn roundtrip() {
let request = CreatedPartitionAssignment {
partition_id: 7,
consensus_group_id: 42,
};
let bytes = request.to_bytes();
let (decoded, consumed) = CreatedPartitionAssignment::decode(&bytes).unwrap();
assert_eq!(consumed, bytes.len());
assert_eq!(decoded, request);
}
}