use crate::{encode::ToByte, error::Result, protocol::HeaderRequest};
const API_KEY_METADATA: i16 = 20;
const API_VERSION: i16 = 3;
#[derive(Debug)]
pub struct DeleteTopicsRequest<'a> {
pub header: HeaderRequest<'a>,
pub topics: Vec<Topic<'a>>,
pub timeout_ms: i32,
}
#[derive(Debug)]
pub struct Topic<'a> {
pub name: &'a str,
}
impl<'a> DeleteTopicsRequest<'a> {
pub fn new(correlation_id: i32, client_id: &'a str, timeout_ms: i32) -> Result<Self> {
let header = HeaderRequest::new(API_KEY_METADATA, API_VERSION, correlation_id, client_id);
Ok(Self {
header,
timeout_ms,
topics: vec![],
})
}
pub fn add(&mut self, topic_name: &'a str) {
match self
.topics
.iter_mut()
.find(|topic| topic.name == topic_name)
{
None => self.topics.push(Topic { name: topic_name }),
Some(_) => {
}
}
}
}
impl ToByte for DeleteTopicsRequest<'_> {
fn encode<T: bytes::BufMut>(&self, buffer: &mut T) -> crate::error::Result<()> {
tracing::trace!("Encoding DeleteTopicsRequest {:?}", self);
self.header.encode(buffer)?;
self.topics.encode(buffer)?;
self.timeout_ms.encode(buffer)?;
Ok(())
}
}
impl ToByte for Topic<'_> {
fn encode<T: bytes::BufMut>(&self, buffer: &mut T) -> crate::error::Result<()> {
self.name.encode(buffer)?;
Ok(())
}
}