use bytes::{BufMut, BytesMut};
use crate::codec::{WireDecode, WireEncode, read_u32_le};
use crate::{WireError, WireIdentifier};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GetPartitionRouteRequest {
pub stream_id: WireIdentifier,
pub topic_id: WireIdentifier,
pub partition_id: u32,
}
impl WireEncode for GetPartitionRouteRequest {
fn encoded_size(&self) -> usize {
self.stream_id.encoded_size() + self.topic_id.encoded_size() + size_of::<u32>()
}
fn encode(&self, buf: &mut BytesMut) {
self.stream_id.encode(buf);
self.topic_id.encode(buf);
buf.put_u32_le(self.partition_id);
}
}
impl WireDecode for GetPartitionRouteRequest {
fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
let (stream_id, stream_size) = WireIdentifier::decode(buf)?;
let (topic_id, topic_size) = WireIdentifier::decode(&buf[stream_size..])?;
let position = stream_size + topic_size;
let partition_id = read_u32_le(buf, position)?;
Ok((
Self {
stream_id,
topic_id,
partition_id,
},
position + size_of::<u32>(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn given_partition_route_request_when_encoded_should_preserve_named_and_numeric_targets() {
for (stream_id, topic_id) in [
(WireIdentifier::numeric(3), WireIdentifier::numeric(7)),
(
WireIdentifier::named("events").unwrap(),
WireIdentifier::named("orders").unwrap(),
),
] {
let request = GetPartitionRouteRequest {
stream_id,
topic_id,
partition_id: 2,
};
let bytes = request.to_bytes();
assert_eq!(bytes.len(), request.encoded_size());
assert_eq!(
GetPartitionRouteRequest::decode_from(&bytes).unwrap(),
request
);
for length in 0..bytes.len() {
assert!(
GetPartitionRouteRequest::decode_from(&bytes[..length]).is_err(),
"accepted a request truncated at {length} bytes"
);
}
}
}
}