Skip to content

Commit 138f17b

Browse files
committed
chore: use derive_builder to reduce boilerplate code
1 parent fb9d1a1 commit 138f17b

File tree

2 files changed

+19
-45
lines changed

2 files changed

+19
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tokio = { version = "1", features = ["rt", "time"] }
1919
tokio-stream = { version = "0.1", features = ["net"] }
2020
tonic = { version = "0.11", features = ["tls", "tls-roots", "gzip", "zstd"] }
2121
tower = "0.4"
22+
derive_builder = "0.20.0"
2223

2324
[build-dependencies]
2425
tonic-build = "0.9"

src/client.rs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use tonic::transport::Channel;
2525

2626
use crate::load_balance::{LoadBalance, Loadbalancer};
2727
use crate::{error, Result};
28+
use derive_builder::Builder;
2829

2930
const MAX_MESSAGE_SIZE: usize = 512 * 1024 * 1024;
3031

@@ -76,7 +77,8 @@ impl ClientBuilder {
7677
.load_balance(self.load_balance)
7778
.compression(self.compression)
7879
.peers(self.peers)
79-
.build();
80+
.build()
81+
.unwrap();
8082
Client {
8183
inner: Arc::new(inner),
8284
}
@@ -87,7 +89,7 @@ impl ClientBuilder {
8789
pub enum Compression {
8890
Gzip,
8991
Zstd,
90-
Plain,
92+
None,
9193
}
9294

9395
impl Default for Compression {
@@ -96,14 +98,22 @@ impl Default for Compression {
9698
}
9799
}
98100

99-
#[derive(Debug, Default)]
101+
#[derive(Debug, Default, Builder)]
100102
struct Inner {
101103
channel_manager: ChannelManager,
104+
#[builder(setter(custom))]
102105
peers: Arc<RwLock<Vec<String>>>,
103106
load_balance: Loadbalancer,
104107
compression: Compression,
105108
}
106109

110+
impl InnerBuilder {
111+
pub fn peers(&mut self, peers: Vec<String>) -> &mut Self {
112+
self.peers = Some(Arc::new(RwLock::new(peers)));
113+
self
114+
}
115+
}
116+
107117
impl Inner {
108118
fn set_peers(&self, peers: Vec<String>) {
109119
let mut guard = self.peers.write();
@@ -116,45 +126,6 @@ impl Inner {
116126
}
117127
}
118128

119-
#[derive(Default)]
120-
pub struct InnerBuilder {
121-
channel_manager: ChannelManager,
122-
load_balance: Loadbalancer,
123-
compression: Compression,
124-
peers: Arc<RwLock<Vec<String>>>,
125-
}
126-
127-
impl InnerBuilder {
128-
pub(self) fn channel_manager(mut self, channel_manager: ChannelManager) -> Self {
129-
self.channel_manager = channel_manager;
130-
self
131-
}
132-
133-
pub(self) fn load_balance(mut self, load_balance: Loadbalancer) -> Self {
134-
self.load_balance = load_balance;
135-
self
136-
}
137-
138-
pub(self) fn compression(mut self, compression: Compression) -> Self {
139-
self.compression = compression;
140-
self
141-
}
142-
143-
pub(self) fn peers(mut self, peers: Vec<String>) -> Self {
144-
self.peers = Arc::new(RwLock::new(peers));
145-
self
146-
}
147-
148-
pub(self) fn build(self) -> Inner {
149-
Inner {
150-
channel_manager: self.channel_manager,
151-
load_balance: self.load_balance,
152-
compression: self.compression,
153-
peers: self.peers,
154-
}
155-
}
156-
}
157-
158129
impl Client {
159130
#[deprecated(since = "0.1.0", note = "use `ClientBuilder` instead of this method")]
160131
pub fn new() -> Self {
@@ -165,7 +136,8 @@ impl Client {
165136
pub fn with_manager(channel_manager: ChannelManager) -> Self {
166137
let inner = InnerBuilder::default()
167138
.channel_manager(channel_manager)
168-
.build();
139+
.build()
140+
.unwrap();
169141
Self {
170142
inner: Arc::new(inner),
171143
}
@@ -189,14 +161,15 @@ impl Client {
189161
let inner = InnerBuilder::default()
190162
.channel_manager(channel_manager)
191163
.peers(normailze_urls(urls))
192-
.build();
164+
.build()
165+
.unwrap();
193166

194167
Self {
195168
inner: Arc::new(inner),
196169
}
197170
}
198171

199-
#[deprecated(since = "0.1.0", note = "should be removed in the future")]
172+
#[deprecated(since = "0.1.0", note = "use `ClientBuilder` instead of this method")]
200173
pub fn start<U, A>(&self, urls: A)
201174
where
202175
U: AsRef<str>,

0 commit comments

Comments
 (0)