Skip to content

Commit

Permalink
Add ClientOptionsBuilder (#210)
Browse files Browse the repository at this point in the history
* add ClientOptionsBuilder
fix clippy warning

* benchmark: fix clippy warning

---------

Co-authored-by: alk888 <aleksei@g.com>
  • Loading branch information
alfredotg and alk888 authored Feb 6, 2024
1 parent 0f44f71 commit 7db8d45
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
4 changes: 2 additions & 2 deletions benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
start_publisher(
environment.clone(),
&opts,
opts.streams.get(0).unwrap().clone(),
opts.streams.first().unwrap().clone(),
)
.await?;

start_consumer(environment, &opts, opts.streams.get(0).unwrap().clone()).await?;
start_consumer(environment, &opts, opts.streams.first().unwrap().clone()).await?;
loop {
tokio::time::sleep(Duration::from_secs(60)).await;
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/message/amqp/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct MessageBody {

impl MessageBody {
pub fn data(&self) -> Option<&Vec<u8>> {
self.data.get(0)
self.data.first()
}

pub fn value(&self) -> Option<&Value> {
Expand Down
92 changes: 92 additions & 0 deletions src/client/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl Default for ClientOptions {
}

impl ClientOptions {
pub fn builder() -> ClientOptionsBuilder {
ClientOptionsBuilder(ClientOptions::default())
}

pub fn get_tls(&self) -> TlsConfiguration {
self.tls.clone()
}
Expand All @@ -64,3 +68,91 @@ impl ClientOptions {
self.port = port;
}
}

pub struct ClientOptionsBuilder(ClientOptions);

impl ClientOptionsBuilder {
pub fn host(mut self, host: &str) -> Self {
self.0.host = host.to_owned();
self
}

pub fn port(mut self, port: u16) -> Self {
self.0.port = port;
self
}

pub fn user(mut self, user: &str) -> Self {
self.0.user = user.to_owned();
self
}

pub fn password(mut self, password: &str) -> Self {
self.0.password = password.to_owned();
self
}

pub fn v_host(mut self, v_host: &str) -> Self {
self.0.v_host = v_host.to_owned();
self
}

pub fn heartbeat(mut self, heartbeat: u32) -> Self {
self.0.heartbeat = heartbeat;
self
}

pub fn max_frame_size(mut self, max_frame_size: u32) -> Self {
self.0.max_frame_size = max_frame_size;
self
}

pub fn tls(mut self, tls: TlsConfiguration) -> Self {
self.0.tls = tls;
self
}

pub fn collector(mut self, collector: Arc<dyn MetricsCollector>) -> Self {
self.0.collector = collector;
self
}

pub fn build(self) -> ClientOptions {
self.0
}
}

#[cfg(test)]
mod tests {
use super::{ClientOptions, NopMetricsCollector, TlsConfiguration};
use std::sync::Arc;

#[test]
fn test_client_options_builder() {
let options = ClientOptions::builder()
.host("test")
.port(8888)
.user("test_user")
.password("test_pass")
.v_host("/test_vhost")
.heartbeat(10000)
.max_frame_size(1)
.tls(TlsConfiguration {
enabled: true,
trust_certificates: false,
root_certificates_path: String::from(""),
client_certificates_path: String::from(""),
client_keys_path: String::from(""),
})
.collector(Arc::new(NopMetricsCollector {}))
.build();
assert_eq!(options.host, "test");
assert_eq!(options.port, 8888);
assert_eq!(options.user, "test_user");
assert_eq!(options.password, "test_pass");
assert_eq!(options.v_host, "/test_vhost");
assert_eq!(options.heartbeat, 10000);
assert_eq!(options.max_frame_size, 1);
assert_eq!(options.tls.enabled, true);
}
}

0 comments on commit 7db8d45

Please sign in to comment.