use bytes::{Buf, BufMut, Bytes, BytesMut};
use crate::error::DecodeError;
use crate::messages::consumer_protocol_assignment::{ConsumerProtocolAssignment, TopicPartition};
use crate::messages::consumer_protocol_subscription::ConsumerProtocolSubscription;
pub fn encode_subscription(topics: &[String]) -> Bytes {
let body = ConsumerProtocolSubscription {
topics: topics.to_vec(),
..ConsumerProtocolSubscription::default()
};
let mut out = BytesMut::new();
out.put_i16(0);
body.encode(&mut out, 0).expect("v0 subscription encodes");
out.freeze()
}
pub fn decode_subscription(data: &[u8]) -> Result<Vec<String>, DecodeError> {
let mut buf = Bytes::copy_from_slice(data);
if buf.len() < 2 {
return Err(DecodeError::Truncated {
needed: 2 - buf.len(),
});
}
let version = buf.get_i16().min(ConsumerProtocolSubscription::MAX_VERSION);
let sub = ConsumerProtocolSubscription::decode(&mut buf, version.max(0))?;
Ok(sub.topics)
}
pub fn encode_assignment(partitions: &[(String, Vec<i32>)]) -> Bytes {
let body = ConsumerProtocolAssignment {
assigned_partitions: partitions
.iter()
.map(|(topic, parts)| TopicPartition {
topic: topic.clone(),
partitions: parts.clone(),
..TopicPartition::default()
})
.collect(),
..ConsumerProtocolAssignment::default()
};
let mut out = BytesMut::new();
out.put_i16(0);
body.encode(&mut out, 0).expect("v0 assignment encodes");
out.freeze()
}
pub fn decode_assignment(data: &[u8]) -> Result<Vec<(String, Vec<i32>)>, DecodeError> {
if data.is_empty() {
return Ok(Vec::new());
}
let mut buf = Bytes::copy_from_slice(data);
if buf.len() < 2 {
return Err(DecodeError::Truncated {
needed: 2 - buf.len(),
});
}
let version = buf.get_i16().min(ConsumerProtocolAssignment::MAX_VERSION);
let assignment = ConsumerProtocolAssignment::decode(&mut buf, version.max(0))?;
Ok(assignment
.assigned_partitions
.into_iter()
.map(|tp| (tp.topic, tp.partitions))
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn payloads_roundtrip() {
let topics = vec!["alpha".to_owned(), "beta".to_owned()];
assert_eq!(
decode_subscription(&encode_subscription(&topics)).unwrap(),
topics
);
let parts = vec![("alpha".to_owned(), vec![0, 2])];
assert_eq!(
decode_assignment(&encode_assignment(&parts)).unwrap(),
parts
);
assert!(decode_assignment(b"").unwrap().is_empty());
}
#[test]
fn newer_envelopes_decode_and_short_ones_error() {
let body = ConsumerProtocolSubscription {
topics: vec!["t".to_owned()],
..ConsumerProtocolSubscription::default()
};
let mut encoded = BytesMut::new();
encoded.put_i16(ConsumerProtocolSubscription::MAX_VERSION + 9);
body.encode(&mut encoded, ConsumerProtocolSubscription::MAX_VERSION)
.unwrap();
encoded.put_slice(b"future-fields");
assert_eq!(decode_subscription(&encoded).unwrap(), vec!["t".to_owned()]);
assert!(decode_subscription(b"\x00").is_err());
assert!(decode_assignment(b"\x00").is_err());
}
}