use crate::WireError;
use crate::WireIdentifier;
use crate::codec::{WireDecode, WireEncode};
use bytes::BytesMut;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JoinConsumerGroupRequest {
pub stream_id: WireIdentifier,
pub topic_id: WireIdentifier,
pub group_id: WireIdentifier,
}
impl WireEncode for JoinConsumerGroupRequest {
fn encoded_size(&self) -> usize {
self.stream_id.encoded_size() + self.topic_id.encoded_size() + self.group_id.encoded_size()
}
fn encode(&self, buf: &mut BytesMut) {
self.stream_id.encode(buf);
self.topic_id.encode(buf);
self.group_id.encode(buf);
}
}
impl WireDecode for JoinConsumerGroupRequest {
fn decode(buf: &[u8]) -> Result<(Self, usize), WireError> {
let mut pos = 0;
let (stream_id, n) = WireIdentifier::decode(&buf[pos..])?;
pos += n;
let (topic_id, n) = WireIdentifier::decode(&buf[pos..])?;
pos += n;
let (group_id, n) = WireIdentifier::decode(&buf[pos..])?;
pos += n;
Ok((
Self {
stream_id,
topic_id,
group_id,
},
pos,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip() {
let req = JoinConsumerGroupRequest {
stream_id: WireIdentifier::numeric(1),
topic_id: WireIdentifier::numeric(2),
group_id: WireIdentifier::numeric(3),
};
let bytes = req.to_bytes();
let (decoded, consumed) = JoinConsumerGroupRequest::decode(&bytes).unwrap();
assert_eq!(consumed, bytes.len());
assert_eq!(decoded, req);
}
#[test]
fn roundtrip_named() {
let req = JoinConsumerGroupRequest {
stream_id: WireIdentifier::named("stream-1").unwrap(),
topic_id: WireIdentifier::named("topic-1").unwrap(),
group_id: WireIdentifier::named("group-1").unwrap(),
};
let bytes = req.to_bytes();
let (decoded, consumed) = JoinConsumerGroupRequest::decode(&bytes).unwrap();
assert_eq!(consumed, bytes.len());
assert_eq!(decoded, req);
}
#[test]
fn truncated_returns_error() {
let req = JoinConsumerGroupRequest {
stream_id: WireIdentifier::numeric(1),
topic_id: WireIdentifier::numeric(2),
group_id: WireIdentifier::numeric(3),
};
let bytes = req.to_bytes();
for i in 0..bytes.len() {
assert!(
JoinConsumerGroupRequest::decode(&bytes[..i]).is_err(),
"expected error for truncation at byte {i}"
);
}
}
}