use std::time::Duration;
use crate::model::common::ClientType;
#[allow(unused_imports)]
use crate::producer::Producer;
#[allow(unused_imports)]
use crate::simple_consumer::SimpleConsumer;
#[derive(Debug, Clone)]
pub struct ClientOption {
pub(crate) client_type: ClientType,
pub(crate) group: Option<String>,
pub(crate) namespace: String,
pub(crate) access_url: String,
pub(crate) enable_tls: bool,
pub(crate) timeout: Duration,
pub(crate) long_polling_timeout: Duration,
pub(crate) access_key: Option<String>,
pub(crate) secret_key: Option<String>,
}
impl Default for ClientOption {
fn default() -> Self {
ClientOption {
client_type: ClientType::Producer,
group: None,
namespace: "".to_string(),
access_url: "localhost:8081".to_string(),
enable_tls: false,
timeout: Duration::from_secs(3),
long_polling_timeout: Duration::from_secs(40),
access_key: None,
secret_key: None,
}
}
}
impl ClientOption {
pub fn access_url(&self) -> &str {
&self.access_url
}
pub fn set_access_url(&mut self, access_url: impl Into<String>) {
self.access_url = access_url.into();
}
pub fn enable_tls(&self) -> bool {
self.enable_tls
}
pub fn set_enable_tls(&mut self, enable_tls: bool) {
self.enable_tls = enable_tls;
}
pub fn timeout(&self) -> &Duration {
&self.timeout
}
pub fn set_timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}
pub fn long_polling_timeout(&self) -> &Duration {
&self.long_polling_timeout
}
pub fn set_long_polling_timeout(&mut self, long_polling_timeout: Duration) {
self.long_polling_timeout = long_polling_timeout;
}
pub fn access_key(&self) -> Option<&String> {
self.access_key.as_ref()
}
pub fn set_access_key(&mut self, access_key: impl Into<String>) {
self.access_key = Some(access_key.into());
}
pub fn secret_key(&self) -> Option<&String> {
self.secret_key.as_ref()
}
pub fn set_secret_key(&mut self, secret_key: impl Into<String>) {
self.secret_key = Some(secret_key.into());
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum LoggingFormat {
Terminal,
Json,
}
#[derive(Debug, Clone)]
pub struct ProducerOption {
logging_format: LoggingFormat,
prefetch_route: bool,
topics: Option<Vec<String>>,
namespace: String,
validate_message_type: bool,
}
impl Default for ProducerOption {
fn default() -> Self {
ProducerOption {
logging_format: LoggingFormat::Terminal,
prefetch_route: true,
topics: None,
namespace: "".to_string(),
validate_message_type: true,
}
}
}
impl ProducerOption {
pub fn logging_format(&self) -> &LoggingFormat {
&self.logging_format
}
pub fn set_logging_format(&mut self, logging_format: LoggingFormat) {
self.logging_format = logging_format;
}
pub fn prefetch_route(&self) -> &bool {
&self.prefetch_route
}
pub fn set_prefetch_route(&mut self, prefetch_route: bool) {
self.prefetch_route = prefetch_route;
}
pub fn topics(&self) -> &Option<Vec<String>> {
&self.topics
}
pub fn set_topics(&mut self, topics: Vec<impl Into<String>>) {
self.topics = Some(topics.into_iter().map(|t| t.into()).collect());
}
pub(crate) fn namespace(&self) -> &str {
&self.namespace
}
pub(crate) fn set_namespace(&mut self, name_space: impl Into<String>) {
self.namespace = name_space.into();
}
pub fn validate_message_type(&self) -> bool {
self.validate_message_type
}
pub fn set_validate_message_type(&mut self, validate_message_type: bool) {
self.validate_message_type = validate_message_type;
}
}
#[derive(Debug, Clone)]
pub struct SimpleConsumerOption {
logging_format: LoggingFormat,
consumer_group: String,
prefetch_route: bool,
topics: Option<Vec<String>>,
namespace: String,
}
impl Default for SimpleConsumerOption {
fn default() -> Self {
SimpleConsumerOption {
logging_format: LoggingFormat::Terminal,
consumer_group: "".to_string(),
prefetch_route: true,
topics: None,
namespace: "".to_string(),
}
}
}
impl SimpleConsumerOption {
pub fn logging_format(&self) -> &LoggingFormat {
&self.logging_format
}
pub fn set_logging_format(&mut self, logging_format: LoggingFormat) {
self.logging_format = logging_format;
}
pub fn consumer_group(&self) -> &str {
&self.consumer_group
}
pub fn set_consumer_group(&mut self, consumer_group: impl Into<String>) {
self.consumer_group = consumer_group.into();
}
pub fn prefetch_route(&self) -> &bool {
&self.prefetch_route
}
pub fn set_prefetch_route(&mut self, prefetch_route: bool) {
self.prefetch_route = prefetch_route;
}
pub fn topics(&self) -> &Option<Vec<String>> {
&self.topics
}
pub fn set_topics(&mut self, topics: Vec<impl Into<String>>) {
self.topics = Some(topics.into_iter().map(|t| t.into()).collect());
}
pub(crate) fn namespace(&self) -> &str {
&self.namespace
}
pub(crate) fn set_namespace(&mut self, name_space: impl Into<String>) {
self.namespace = name_space.into();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conf_client_option() {
let option = ClientOption::default();
assert_eq!(option.access_url(), "localhost:8081");
assert!(!option.enable_tls());
assert_eq!(option.timeout(), &Duration::from_secs(3));
assert_eq!(option.long_polling_timeout(), &Duration::from_secs(40));
}
#[test]
fn conf_producer_option() {
let option = ProducerOption::default();
assert_eq!(option.logging_format(), &LoggingFormat::Terminal);
assert!(option.prefetch_route());
assert!(option.validate_message_type());
}
#[test]
fn conf_simple_consumer_option() {
let option = SimpleConsumerOption::default();
assert_eq!(option.logging_format(), &LoggingFormat::Terminal);
assert!(option.prefetch_route());
}
}