Skip to content

Commit 6aa409f

Browse files
committed
Simplify ConnectionBuilder impl
1 parent 87e99e9 commit 6aa409f

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/lib.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ pub type SkyResult<T> = Result<T, self::error::Error>;
216216
/// A result type for queries
217217
pub type SkyQueryResult = SkyResult<Element>;
218218

219-
#[derive(Debug, Clone)]
220219
/// A connection builder for easily building connections
221220
///
222221
/// ## Example (sync)
@@ -245,10 +244,11 @@ pub type SkyQueryResult = SkyResult<Element>;
245244
/// .unwrap();
246245
/// }
247246
/// ```
247+
#[derive(Debug, Clone)]
248248
pub struct ConnectionBuilder {
249-
port: Option<u16>,
250-
host: Option<String>,
251-
entity: Option<String>,
249+
port: u16,
250+
host: String,
251+
entity: String,
252252
}
253253

254254
impl Default for ConnectionBuilder {
@@ -260,34 +260,30 @@ impl Default for ConnectionBuilder {
260260
impl ConnectionBuilder {
261261
/// Create an empty connection builder
262262
pub fn new() -> Self {
263-
Self {
264-
port: None,
265-
host: None,
266-
entity: None,
267-
}
263+
Self::default()
268264
}
269265
/// Set the port (defaults to `2003`)
270266
pub fn set_port(mut self, port: u16) -> Self {
271-
self.port = Some(port);
267+
self.port = port;
272268
self
273269
}
274270
/// Set the host (defaults to `localhost`)
275271
pub fn set_host(mut self, host: String) -> Self {
276-
self.host = Some(host);
272+
self.host = host;
277273
self
278274
}
279275
/// Set the entity (defaults to `default:default`)
280276
pub fn set_entity(mut self, entity: String) -> Self {
281-
self.entity = Some(entity);
277+
self.entity = entity;
282278
self
283279
}
284280
cfg_sync! {
285281
/// Get a [sync connection](sync::Connection) to the database
286282
pub fn get_connection(&self) -> SkyResult<sync::Connection> {
287283
use crate::ddl::Ddl;
288284
let mut con =
289-
sync::Connection::new(self.host.as_ref().unwrap_or(&DEFAULT_HOSTADDR.to_owned()), self.port.unwrap_or(2003))?;
290-
con.switch(self.entity.as_ref().unwrap_or(&DEFAULT_ENTITY.to_owned()))?;
285+
sync::Connection::new(&self.host, self.port)?;
286+
con.switch(&self.entity)?;
291287
Ok(con)
292288
}
293289
cfg_sync_ssl_any! {
@@ -298,11 +294,11 @@ impl ConnectionBuilder {
298294
) -> SkyResult<sync::TlsConnection> {
299295
use crate::ddl::Ddl;
300296
let mut con = sync::TlsConnection::new(
301-
self.host.as_ref().unwrap_or(&DEFAULT_HOSTADDR.to_owned()),
302-
self.port.unwrap_or(2003),
297+
&self.host,
298+
self.port,
303299
&sslcert,
304300
)?;
305-
con.switch(self.entity.as_ref().unwrap_or(&DEFAULT_ENTITY.to_owned()))?;
301+
con.switch(&self.entity)?;
306302
Ok(con)
307303
}
308304
}
@@ -311,9 +307,9 @@ impl ConnectionBuilder {
311307
/// Get an [async connection](aio::Connection) to the database
312308
pub async fn get_async_connection(&self) -> SkyResult<aio::Connection> {
313309
use crate::ddl::AsyncDdl;
314-
let mut con = aio::Connection::new(self.host.as_ref().unwrap_or(&DEFAULT_HOSTADDR.to_owned()), self.port.unwrap_or(2003))
310+
let mut con = aio::Connection::new(&self.host, self.port)
315311
.await?;
316-
con.switch(self.entity.as_ref().unwrap_or(&DEFAULT_ENTITY.to_owned())).await?;
312+
con.switch(&self.entity).await?;
317313
Ok(con)
318314
}
319315
cfg_async_ssl_any! {
@@ -324,12 +320,12 @@ impl ConnectionBuilder {
324320
) -> SkyResult<aio::TlsConnection> {
325321
use crate::ddl::AsyncDdl;
326322
let mut con = aio::TlsConnection::new(
327-
self.host.as_ref().unwrap_or(&DEFAULT_HOSTADDR.to_owned()),
328-
self.port.unwrap_or(2003),
323+
&self.host,
324+
self.port,
329325
&sslcert,
330326
)
331327
.await?;
332-
con.switch(self.entity.as_ref().unwrap_or(&DEFAULT_ENTITY.to_owned())).await?;
328+
con.switch(&self.entity).await?;
333329
Ok(con)
334330
}
335331
}

0 commit comments

Comments
 (0)