Skip to content

Commit b73ec01

Browse files
committed
Expose accessors on Config object
Fix sfackler#534.
1 parent 2093f3e commit b73ec01

File tree

2 files changed

+151
-2
lines changed

2 files changed

+151
-2
lines changed

postgres/src/config.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::str::FromStr;
1111
use std::time::Duration;
1212
use tokio::runtime;
1313
#[doc(inline)]
14-
pub use tokio_postgres::config::{ChannelBinding, SslMode, TargetSessionAttrs};
14+
pub use tokio_postgres::config::{ChannelBinding, Host, SslMode, TargetSessionAttrs};
1515
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
1616
use tokio_postgres::{Error, Socket};
1717

@@ -123,6 +123,12 @@ impl Config {
123123
self
124124
}
125125

126+
/// Gets the user to authenticate with, if one has been configured with
127+
/// the `user` method.
128+
pub fn get_user(&self) -> Option<&str> {
129+
self.config.get_user()
130+
}
131+
126132
/// Sets the password to authenticate with.
127133
pub fn password<T>(&mut self, password: T) -> &mut Config
128134
where
@@ -132,6 +138,12 @@ impl Config {
132138
self
133139
}
134140

141+
/// Gets the password to authenticate with, if one has been configured with
142+
/// the `password` method.
143+
pub fn get_password(&self) -> Option<&[u8]> {
144+
self.config.get_password()
145+
}
146+
135147
/// Sets the name of the database to connect to.
136148
///
137149
/// Defaults to the user.
@@ -140,18 +152,36 @@ impl Config {
140152
self
141153
}
142154

155+
/// Gets the name of the database to connect to, if one has been configured
156+
/// with the `dbname` method.
157+
pub fn get_dbname(&self) -> Option<&str> {
158+
self.config.get_dbname()
159+
}
160+
143161
/// Sets command line options used to configure the server.
144162
pub fn options(&mut self, options: &str) -> &mut Config {
145163
self.config.options(options);
146164
self
147165
}
148166

167+
/// Gets the command line options used to configure the server, if the
168+
/// options have been set with the `options` method.
169+
pub fn get_options(&self) -> Option<&str> {
170+
self.config.get_options()
171+
}
172+
149173
/// Sets the value of the `application_name` runtime parameter.
150174
pub fn application_name(&mut self, application_name: &str) -> &mut Config {
151175
self.config.application_name(application_name);
152176
self
153177
}
154178

179+
/// Gets the value of the `application_name` runtime parameter, if it has
180+
/// been set with the `application_name` method.
181+
pub fn get_application_name(&self) -> Option<&str> {
182+
self.config.get_application_name()
183+
}
184+
155185
/// Sets the SSL configuration.
156186
///
157187
/// Defaults to `prefer`.
@@ -160,6 +190,11 @@ impl Config {
160190
self
161191
}
162192

193+
/// Gets the SSL configuration.
194+
pub fn get_ssl_mode(&self) -> SslMode {
195+
self.config.get_ssl_mode()
196+
}
197+
163198
/// Adds a host to the configuration.
164199
///
165200
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
@@ -169,6 +204,11 @@ impl Config {
169204
self
170205
}
171206

207+
/// Gets the hosts that have been added to the configuration with `host`.
208+
pub fn get_hosts(&self) -> &[Host] {
209+
self.config.get_hosts()
210+
}
211+
172212
/// Adds a Unix socket host to the configuration.
173213
///
174214
/// Unlike `host`, this method allows non-UTF8 paths.
@@ -191,6 +231,11 @@ impl Config {
191231
self
192232
}
193233

234+
/// Gets the ports that have been added to the configuration with `port`.
235+
pub fn get_ports(&self) -> &[u16] {
236+
self.config.get_ports()
237+
}
238+
194239
/// Sets the timeout applied to socket-level connection attempts.
195240
///
196241
/// Note that hostnames can resolve to multiple IP addresses, and this timeout will apply to each address of each
@@ -200,6 +245,12 @@ impl Config {
200245
self
201246
}
202247

248+
/// Gets the connection timeout, if one has been set with the
249+
/// `connect_timeout` method.
250+
pub fn get_connect_timeout(&self) -> Option<&Duration> {
251+
self.config.get_connect_timeout()
252+
}
253+
203254
/// Controls the use of TCP keepalive.
204255
///
205256
/// This is ignored for Unix domain socket connections. Defaults to `true`.
@@ -208,6 +259,11 @@ impl Config {
208259
self
209260
}
210261

262+
/// Reports whether TCP keepalives will be used.
263+
pub fn get_keepalives(&self) -> bool {
264+
self.config.get_keepalives()
265+
}
266+
211267
/// Sets the amount of idle time before a keepalive packet is sent on the connection.
212268
///
213269
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
@@ -216,6 +272,12 @@ impl Config {
216272
self
217273
}
218274

275+
/// Gets the configured amount of idle time before a keepalive packet will
276+
/// be sent on the connection.
277+
pub fn get_keepalives_idle(&self) -> Duration {
278+
self.config.get_keepalives_idle()
279+
}
280+
219281
/// Sets the requirements of the session.
220282
///
221283
/// This can be used to connect to the primary server in a clustered database rather than one of the read-only
@@ -228,6 +290,11 @@ impl Config {
228290
self
229291
}
230292

293+
/// Gets the requirements of the session.
294+
pub fn get_target_session_attrs(&self) -> TargetSessionAttrs {
295+
self.config.get_target_session_attrs()
296+
}
297+
231298
/// Sets the channel binding behavior.
232299
///
233300
/// Defaults to `prefer`.
@@ -236,6 +303,11 @@ impl Config {
236303
self
237304
}
238305

306+
/// Gets the channel binding behavior.
307+
pub fn get_channel_binding(&self) -> ChannelBinding {
308+
self.config.get_channel_binding()
309+
}
310+
239311
/// Opens a connection to a PostgreSQL database.
240312
pub fn connect<T>(&self, tls: T) -> Result<Client, Error>
241313
where

tokio-postgres/src/config.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,14 @@ pub enum ChannelBinding {
5656
Require,
5757
}
5858

59+
/// A host specification.
5960
#[derive(Debug, Clone, PartialEq)]
60-
pub(crate) enum Host {
61+
pub enum Host {
62+
/// A TCP hostname.
6163
Tcp(String),
64+
/// A path to a directory containing the server's Unix socket.
65+
///
66+
/// This variant is only available on Unix platforms.
6267
#[cfg(unix)]
6368
Unix(PathBuf),
6469
}
@@ -190,6 +195,12 @@ impl Config {
190195
self
191196
}
192197

198+
/// Gets the user to authenticate with, if one has been configured with
199+
/// the `user` method.
200+
pub fn get_user(&self) -> Option<&str> {
201+
self.user.as_deref()
202+
}
203+
193204
/// Sets the password to authenticate with.
194205
pub fn password<T>(&mut self, password: T) -> &mut Config
195206
where
@@ -199,6 +210,12 @@ impl Config {
199210
self
200211
}
201212

213+
/// Gets the password to authenticate with, if one has been configured with
214+
/// the `password` method.
215+
pub fn get_password(&self) -> Option<&[u8]> {
216+
self.password.as_deref()
217+
}
218+
202219
/// Sets the name of the database to connect to.
203220
///
204221
/// Defaults to the user.
@@ -207,18 +224,36 @@ impl Config {
207224
self
208225
}
209226

227+
/// Gets the name of the database to connect to, if one has been configured
228+
/// with the `dbname` method.
229+
pub fn get_dbname(&self) -> Option<&str> {
230+
self.dbname.as_deref()
231+
}
232+
210233
/// Sets command line options used to configure the server.
211234
pub fn options(&mut self, options: &str) -> &mut Config {
212235
self.options = Some(options.to_string());
213236
self
214237
}
215238

239+
/// Gets the command line options used to configure the server, if the
240+
/// options have been set with the `options` method.
241+
pub fn get_options(&self) -> Option<&str> {
242+
self.options.as_deref()
243+
}
244+
216245
/// Sets the value of the `application_name` runtime parameter.
217246
pub fn application_name(&mut self, application_name: &str) -> &mut Config {
218247
self.application_name = Some(application_name.to_string());
219248
self
220249
}
221250

251+
/// Gets the value of the `application_name` runtime parameter, if it has
252+
/// been set with the `application_name` method.
253+
pub fn get_application_name(&self) -> Option<&str> {
254+
self.application_name.as_deref()
255+
}
256+
222257
/// Sets the SSL configuration.
223258
///
224259
/// Defaults to `prefer`.
@@ -227,6 +262,11 @@ impl Config {
227262
self
228263
}
229264

265+
/// Gets the SSL configuration.
266+
pub fn get_ssl_mode(&self) -> SslMode {
267+
self.ssl_mode
268+
}
269+
230270
/// Adds a host to the configuration.
231271
///
232272
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
@@ -243,6 +283,11 @@ impl Config {
243283
self
244284
}
245285

286+
/// Gets the hosts that have been added to the configuration with `host`.
287+
pub fn get_hosts(&self) -> &[Host] {
288+
&self.host
289+
}
290+
246291
/// Adds a Unix socket host to the configuration.
247292
///
248293
/// Unlike `host`, this method allows non-UTF8 paths.
@@ -265,6 +310,11 @@ impl Config {
265310
self
266311
}
267312

313+
/// Gets the ports that have been added to the configuration with `port`.
314+
pub fn get_ports(&self) -> &[u16] {
315+
&self.port
316+
}
317+
268318
/// Sets the timeout applied to socket-level connection attempts.
269319
///
270320
/// Note that hostnames can resolve to multiple IP addresses, and this timeout will apply to each address of each
@@ -274,6 +324,12 @@ impl Config {
274324
self
275325
}
276326

327+
/// Gets the connection timeout, if one has been set with the
328+
/// `connect_timeout` method.
329+
pub fn get_connect_timeout(&self) -> Option<&Duration> {
330+
self.connect_timeout.as_ref()
331+
}
332+
277333
/// Controls the use of TCP keepalive.
278334
///
279335
/// This is ignored for Unix domain socket connections. Defaults to `true`.
@@ -282,6 +338,11 @@ impl Config {
282338
self
283339
}
284340

341+
/// Reports whether TCP keepalives will be used.
342+
pub fn get_keepalives(&self) -> bool {
343+
self.keepalives
344+
}
345+
285346
/// Sets the amount of idle time before a keepalive packet is sent on the connection.
286347
///
287348
/// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
@@ -290,6 +351,12 @@ impl Config {
290351
self
291352
}
292353

354+
/// Gets the configured amount of idle time before a keepalive packet will
355+
/// be sent on the connection.
356+
pub fn get_keepalives_idle(&self) -> Duration {
357+
self.keepalives_idle
358+
}
359+
293360
/// Sets the requirements of the session.
294361
///
295362
/// This can be used to connect to the primary server in a clustered database rather than one of the read-only
@@ -302,6 +369,11 @@ impl Config {
302369
self
303370
}
304371

372+
/// Gets the requirements of the session.
373+
pub fn get_target_session_attrs(&self) -> TargetSessionAttrs {
374+
self.target_session_attrs
375+
}
376+
305377
/// Sets the channel binding behavior.
306378
///
307379
/// Defaults to `prefer`.
@@ -310,6 +382,11 @@ impl Config {
310382
self
311383
}
312384

385+
/// Gets the channel binding behavior.
386+
pub fn get_channel_binding(&self) -> ChannelBinding {
387+
self.channel_binding
388+
}
389+
313390
fn param(&mut self, key: &str, value: &str) -> Result<(), Error> {
314391
match key {
315392
"user" => {

0 commit comments

Comments
 (0)