From 4a0bc679a918fdf55f6d34ac675b3981a9885a56 Mon Sep 17 00:00:00 2001 From: paomian Date: Tue, 21 May 2024 21:12:07 +0800 Subject: [PATCH] chore: use derive_builder to reduce boilerplate code --- Cargo.toml | 1 + src/client.rs | 65 +++++++++++++++------------------------------------ 2 files changed, 20 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 745f93a..64332a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ tokio = { version = "1", features = ["rt", "time"] } tokio-stream = { version = "0.1", features = ["net"] } tonic = { version = "0.11", features = ["tls", "tls-roots", "gzip", "zstd"] } tower = "0.4" +derive_builder = "0.20.0" [build-dependencies] tonic-build = "0.9" diff --git a/src/client.rs b/src/client.rs index 79c2865..6935339 100644 --- a/src/client.rs +++ b/src/client.rs @@ -25,6 +25,7 @@ use tonic::transport::Channel; use crate::load_balance::{LoadBalance, Loadbalancer}; use crate::{error, Result}; +use derive_builder::Builder; const MAX_MESSAGE_SIZE: usize = 512 * 1024 * 1024; @@ -76,7 +77,8 @@ impl ClientBuilder { .load_balance(self.load_balance) .compression(self.compression) .peers(self.peers) - .build(); + .build() + .unwrap(); Client { inner: Arc::new(inner), } @@ -87,7 +89,7 @@ impl ClientBuilder { pub enum Compression { Gzip, Zstd, - Plain, + None, } impl Default for Compression { @@ -96,14 +98,22 @@ impl Default for Compression { } } -#[derive(Debug, Default)] +#[derive(Debug, Default, Builder)] struct Inner { channel_manager: ChannelManager, + #[builder(setter(custom))] peers: Arc>>, load_balance: Loadbalancer, compression: Compression, } +impl InnerBuilder { + pub fn peers(&mut self, peers: Vec) -> &mut Self { + self.peers = Some(Arc::new(RwLock::new(peers))); + self + } +} + impl Inner { fn set_peers(&self, peers: Vec) { let mut guard = self.peers.write(); @@ -116,45 +126,6 @@ impl Inner { } } -#[derive(Default)] -pub struct InnerBuilder { - channel_manager: ChannelManager, - load_balance: Loadbalancer, - compression: Compression, - peers: Arc>>, -} - -impl InnerBuilder { - pub(self) fn channel_manager(mut self, channel_manager: ChannelManager) -> Self { - self.channel_manager = channel_manager; - self - } - - pub(self) fn load_balance(mut self, load_balance: Loadbalancer) -> Self { - self.load_balance = load_balance; - self - } - - pub(self) fn compression(mut self, compression: Compression) -> Self { - self.compression = compression; - self - } - - pub(self) fn peers(mut self, peers: Vec) -> Self { - self.peers = Arc::new(RwLock::new(peers)); - self - } - - pub(self) fn build(self) -> Inner { - Inner { - channel_manager: self.channel_manager, - load_balance: self.load_balance, - compression: self.compression, - peers: self.peers, - } - } -} - impl Client { #[deprecated(since = "0.1.0", note = "use `ClientBuilder` instead of this method")] pub fn new() -> Self { @@ -165,7 +136,8 @@ impl Client { pub fn with_manager(channel_manager: ChannelManager) -> Self { let inner = InnerBuilder::default() .channel_manager(channel_manager) - .build(); + .build() + .unwrap(); Self { inner: Arc::new(inner), } @@ -189,14 +161,15 @@ impl Client { let inner = InnerBuilder::default() .channel_manager(channel_manager) .peers(normailze_urls(urls)) - .build(); + .build() + .unwrap(); Self { inner: Arc::new(inner), } } - #[deprecated(since = "0.1.0", note = "should be removed in the future")] + #[deprecated(since = "0.1.0", note = "use `ClientBuilder` instead of this method")] pub fn start(&self, urls: A) where U: AsRef, @@ -233,7 +206,7 @@ impl Client { Compression::Zstd => { client = client.send_compressed(CompressionEncoding::Zstd); } - Compression::Plain => {} + Compression::None => {} } Ok(DatabaseClient { inner: client }) }