use tansu_client::{Client, ConnectionManager};
use tansu_sans_io::{ErrorCode, MetadataRequest, MetadataResponse};
use tracing::debug;
use url::Url;
use crate::{Error, Result, Topic};
#[derive(Clone, Debug, Default)]
pub struct Builder<B> {
broker: B,
}
impl<B> Builder<B> {
pub fn broker(self, broker: Url) -> Builder<Url> {
Builder { broker }
}
}
impl Builder<Url> {
pub fn build(self) -> Topic {
Topic::List(Configuration {
broker: self.broker,
})
}
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Configuration {
broker: Url,
}
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct List {
configuration: Configuration,
}
impl TryFrom<Configuration> for List {
type Error = Error;
fn try_from(configuration: Configuration) -> Result<Self, Self::Error> {
Ok(List { configuration })
}
}
impl List {
pub(crate) async fn main(self) -> Result<ErrorCode> {
let client = ConnectionManager::builder(self.configuration.broker.clone())
.client_id(Some(env!("CARGO_PKG_NAME").into()))
.build()
.await
.inspect(|pool| debug!(?pool))
.map(Client::new)?;
let req = MetadataRequest::default()
.allow_auto_topic_creation(Some(false))
.include_cluster_authorized_operations(Some(false))
.include_topic_authorized_operations(Some(false))
.topics(None);
let MetadataResponse { topics, .. } = client
.call(req)
.await
.inspect(|response| debug!(?response))?;
serde_json::to_string(topics.as_deref().unwrap_or_default())
.inspect(|topics| println!("{topics}"))
.map_err(Into::into)
.and(Ok(ErrorCode::None))
}
}