blob: bb6faf6d282705c0c4856fb85241834a9c8c75ff [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::model::common::ClientType;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct ClientOption {
pub(crate) client_type: ClientType,
pub(crate) group: 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,
}
impl Default for ClientOption {
fn default() -> Self {
ClientOption {
client_type: ClientType::Producer,
group: "".to_string(),
namespace: "".to_string(),
access_url: "localhost:8081".to_string(),
enable_tls: true,
timeout: Duration::from_secs(3),
long_polling_timeout: Duration::from_secs(40),
}
}
}
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;
}
}
#[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());
}
// not expose to user for now
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());
}
// not expose to user for now
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());
}
}