use std::{num::NonZeroU16, time::Duration};
#[derive(Debug, Clone)]
pub struct KafkaConfig {
pub brokers: Vec<String>,
pub client_id: String,
pub partitions: NonZeroU16,
pub(crate) backoff_deadline: Option<Duration>,
}
impl KafkaConfig {
pub fn new(brokers: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self {
brokers: brokers.into_iter().map(Into::into).collect(),
client_id: "reinhardt".to_owned(),
partitions: NonZeroU16::new(1).expect("1 is non-zero"),
backoff_deadline: None,
}
}
pub fn with_client_id(mut self, id: impl Into<String>) -> Self {
self.client_id = id.into();
self
}
pub fn with_partitions(mut self, n: NonZeroU16) -> Self {
self.partitions = n;
self
}
pub fn with_backoff_deadline(mut self, deadline: Duration) -> Self {
self.backoff_deadline = Some(deadline);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
fn default_client_id_is_reinhardt() {
let config = KafkaConfig::new(["localhost:9092"]);
assert_eq!(config.client_id, "reinhardt");
}
#[rstest]
fn brokers_are_stored() {
let config = KafkaConfig::new(["a:9092", "b:9092"]);
assert_eq!(config.brokers, vec!["a:9092", "b:9092"]);
}
#[rstest]
fn builder_overrides_client_id() {
let config = KafkaConfig::new(["localhost:9092"]).with_client_id("my-app");
assert_eq!(config.client_id, "my-app");
}
#[rstest]
fn default_partitions_is_one() {
let config = KafkaConfig::new(["localhost:9092"]);
assert_eq!(config.partitions.get(), 1);
}
#[rstest]
fn builder_overrides_partitions() {
let config = KafkaConfig::new(["localhost:9092"])
.with_partitions(NonZeroU16::new(4).expect("4 is non-zero"));
assert_eq!(config.partitions.get(), 4);
}
}