Skip to content

Commit 4dad182

Browse files
s1ckknutwalker
andauthored
Refactor Config and ConfigBuilder (#71)
* Replace &str with Into<String> in ConfigBuilder Co-Authored-By: Paul Horn <dev@knutwalker.engineer> * Use AsRef<str> in Graph constructor Co-Authored-By: Paul Horn <dev@knutwalker.engineer> * Implement Default for ConfigBuilder * Improve docs in config.rs * Fix docs in Graph * Improve ConfigBuilder::build * Use `neo4j` as default database name * Add GraphConfig::new and improve docs a bit more. * Remove unnecessary 'static qualifier from const * Remove Option from parameters where we provide a default * Replace async tests with sync tests * Use same Into<String> bound in Graph as in Config --------- Co-authored-by: Paul Horn <dev@knutwalker.engineer>
1 parent ff4ccf5 commit 4dad182

File tree

6 files changed

+89
-72
lines changed

6 files changed

+89
-72
lines changed

lib/src/config.rs

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
pub use crate::errors::*;
22

3+
const DEFAULT_DATABASE: &str = "neo4j";
34
const DEFAULT_FETCH_SIZE: usize = 200;
45
const DEFAULT_MAX_CONNECTIONS: usize = 16;
56

6-
/// The configuration used to connect to the database, see [`crate::Graph::connect`]
7+
/// The configuration used to connect to the database, see [`crate::Graph::connect`].
78
#[derive(Debug, Clone)]
89
pub struct Config {
910
pub(crate) uri: String,
@@ -14,97 +15,101 @@ pub struct Config {
1415
pub(crate) fetch_size: usize,
1516
}
1617

17-
/// A builder to override default configurations and build the [`Config`]
18+
/// A builder to override default configurations and build the [`Config`].
1819
pub struct ConfigBuilder {
1920
uri: Option<String>,
2021
user: Option<String>,
2122
password: Option<String>,
22-
db: Option<String>,
23-
fetch_size: Option<usize>,
24-
max_connections: Option<usize>,
23+
db: String,
24+
fetch_size: usize,
25+
max_connections: usize,
2526
}
2627

2728
impl ConfigBuilder {
28-
///the uri of the neo4j server
29-
pub fn uri(mut self, uri: &str) -> Self {
30-
self.uri = Some(uri.to_owned());
29+
/// Creates a new `ConfigBuilder` with default values.
30+
pub fn new() -> Self {
31+
Self::default()
32+
}
33+
34+
/// The uri of the Neo4j server, e.g. "127.0.0.1:7687".
35+
pub fn uri(mut self, uri: impl Into<String>) -> Self {
36+
self.uri = Some(uri.into());
3137
self
3238
}
3339

34-
///username for authentication
35-
pub fn user(mut self, user: &str) -> Self {
36-
self.user = Some(user.to_owned());
40+
/// The username for authenticating with the Neo4j server.
41+
pub fn user(mut self, user: impl Into<String>) -> Self {
42+
self.user = Some(user.into());
3743
self
3844
}
3945

40-
///password for authentication
41-
pub fn password(mut self, password: &str) -> Self {
42-
self.password = Some(password.to_owned());
46+
/// The password for authenticating with the Neo4j server.
47+
pub fn password(mut self, password: impl Into<String>) -> Self {
48+
self.password = Some(password.into());
4349
self
4450
}
4551

46-
///the name of the database, defaults to "neo4j" if not configured.
47-
pub fn db(mut self, db: &str) -> Self {
48-
self.db = Some(db.to_owned());
52+
/// The name of the database to connect to.
53+
///
54+
/// Defaults to "neo4j" if not set.
55+
pub fn db(mut self, db: impl Into<String>) -> Self {
56+
self.db = db.into();
4957
self
5058
}
5159

52-
///fetch_size indicates the number of rows to fetch from server in one request, it is
53-
///recommended to use a large fetch_size if you are working with large data sets.
54-
///default fetch_size is 200
60+
/// `fetch_size` indicates the number of rows to fetch from server in one request.
61+
/// It is recommended to use a large `fetch_size` if you are working with large data sets.
62+
///
63+
/// Defaults to 200 if not set.
5564
pub fn fetch_size(mut self, fetch_size: usize) -> Self {
56-
self.fetch_size = Some(fetch_size);
65+
self.fetch_size = fetch_size;
5766
self
5867
}
5968

60-
///maximum number of connections in the connection pool
69+
/// The maximum number of connections in the connection pool.
70+
///
71+
/// Defaults to 16 if not set.
6172
pub fn max_connections(mut self, max_connections: usize) -> Self {
62-
self.max_connections = Some(max_connections);
73+
self.max_connections = max_connections;
6374
self
6475
}
6576

6677
pub fn build(self) -> Result<Config> {
67-
if self.uri.is_none()
68-
|| self.user.is_none()
69-
|| self.password.is_none()
70-
|| self.fetch_size.is_none()
71-
|| self.max_connections.is_none()
72-
|| self.db.is_none()
73-
{
74-
Err(Error::InvalidConfig)
75-
} else {
76-
//The config attributes are validated before unwrapping
78+
if let (Some(uri), Some(user), Some(password)) = (self.uri, self.user, self.password) {
7779
Ok(Config {
78-
uri: self.uri.unwrap(),
79-
user: self.user.unwrap(),
80-
password: self.password.unwrap(),
81-
fetch_size: self.fetch_size.unwrap(),
82-
max_connections: self.max_connections.unwrap(),
83-
db: self.db.unwrap(),
80+
uri,
81+
user,
82+
password,
83+
fetch_size: self.fetch_size,
84+
max_connections: self.max_connections,
85+
db: self.db,
8486
})
87+
} else {
88+
Err(Error::InvalidConfig)
8589
}
8690
}
8791
}
8892

89-
/// Creates a config builder with reasonable default values wherever appropriate.
90-
pub fn config() -> ConfigBuilder {
91-
ConfigBuilder {
92-
uri: None,
93-
user: None,
94-
password: None,
95-
db: Some("".to_owned()),
96-
max_connections: Some(DEFAULT_MAX_CONNECTIONS),
97-
fetch_size: Some(DEFAULT_FETCH_SIZE),
93+
impl Default for ConfigBuilder {
94+
fn default() -> Self {
95+
ConfigBuilder {
96+
uri: None,
97+
user: None,
98+
password: None,
99+
db: DEFAULT_DATABASE.into(),
100+
max_connections: DEFAULT_MAX_CONNECTIONS,
101+
fetch_size: DEFAULT_FETCH_SIZE,
102+
}
98103
}
99104
}
100105

101106
#[cfg(test)]
102107
mod tests {
103108
use super::*;
104109

105-
#[tokio::test]
106-
async fn should_build_config() {
107-
let config = config()
110+
#[test]
111+
fn should_build_config() {
112+
let config = ConfigBuilder::default()
108113
.uri("127.0.0.1:7687")
109114
.user("some_user")
110115
.password("some_password")
@@ -121,9 +126,9 @@ mod tests {
121126
assert_eq!(config.max_connections, 5);
122127
}
123128

124-
#[tokio::test]
125-
async fn should_build_with_defaults() {
126-
let config = config()
129+
#[test]
130+
fn should_build_with_defaults() {
131+
let config = ConfigBuilder::default()
127132
.uri("127.0.0.1:7687")
128133
.user("some_user")
129134
.password("some_password")
@@ -132,26 +137,26 @@ mod tests {
132137
assert_eq!(config.uri, "127.0.0.1:7687");
133138
assert_eq!(config.user, "some_user");
134139
assert_eq!(config.password, "some_password");
135-
assert_eq!(config.db, "");
140+
assert_eq!(config.db, "neo4j");
136141
assert_eq!(config.fetch_size, 200);
137142
assert_eq!(config.max_connections, 16);
138143
}
139144

140-
#[tokio::test]
141-
async fn should_reject_invalid_config() {
142-
assert!(config()
145+
#[test]
146+
fn should_reject_invalid_config() {
147+
assert!(ConfigBuilder::default()
143148
.user("some_user")
144149
.password("some_password")
145150
.build()
146151
.is_err());
147152

148-
assert!(config()
153+
assert!(ConfigBuilder::default()
149154
.uri("127.0.0.1:7687")
150155
.password("some_password")
151156
.build()
152157
.is_err());
153158

154-
assert!(config()
159+
assert!(ConfigBuilder::default()
155160
.uri("127.0.0.1:7687")
156161
.user("some_user")
157162
.build()

lib/src/graph.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::{config, Config};
1+
use crate::config::{Config, ConfigBuilder};
22
use crate::errors::*;
33
use crate::pool::{create_pool, ConnectionPool};
44
use crate::query::Query;
@@ -19,16 +19,25 @@ pub fn query(q: &str) -> Query {
1919
}
2020

2121
impl Graph {
22-
/// Connects to the database with configurations provided, you can build a config using
23-
/// [`config`]
22+
/// Connects to the database with configurations provided.
23+
///
24+
/// You can build a config using [`ConfigBuilder::default()`].
2425
pub async fn connect(config: Config) -> Result<Self> {
2526
let pool = create_pool(&config).await?;
2627
Ok(Graph { config, pool })
2728
}
2829

2930
/// Connects to the database with default configurations
30-
pub async fn new(uri: &str, user: &str, password: &str) -> Result<Self> {
31-
let config = config().uri(uri).user(user).password(password).build()?;
31+
pub async fn new(
32+
uri: impl Into<String>,
33+
user: impl Into<String>,
34+
password: impl Into<String>,
35+
) -> Result<Self> {
36+
let config = ConfigBuilder::default()
37+
.uri(uri)
38+
.user(user)
39+
.password(password)
40+
.build()?;
3241
Self::connect(config).await
3342
}
3443

lib/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
//!
6767
//! #[tokio::main]
6868
//! async fn main() {
69-
//! let config = config()
69+
//! let config = ConfigBuilder::default()
7070
//! .uri("127.0.0.1:7687")
7171
//! .user("neo4j")
7272
//! .password("neo")
@@ -172,7 +172,7 @@
172172
//!
173173
//! #[tokio::main]
174174
//! async fn main() {
175-
//! let config = config()
175+
//! let config = ConfigBuilder::default()
176176
//! .uri("127.0.0.1:7687")
177177
//! .user("neo4j")
178178
//! .password("neo")
@@ -764,7 +764,7 @@ mod txn;
764764
mod types;
765765
mod version;
766766

767-
pub use crate::config::{config, Config, ConfigBuilder};
767+
pub use crate::config::{Config, ConfigBuilder};
768768
pub use crate::errors::*;
769769
pub use crate::graph::{query, Graph};
770770
pub use crate::query::Query;

lib/tests/configurations.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ mod container;
44

55
#[tokio::test]
66
async fn configurations() {
7-
let config = config().db("neo4j").fetch_size(500).max_connections(10);
7+
let config = ConfigBuilder::default()
8+
.db("neo4j")
9+
.fetch_size(500)
10+
.max_connections(10);
811
let neo4j = container::Neo4jContainer::from_config(config).await;
912
let graph = neo4j.graph();
1013

lib/tests/container.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use lenient_semver::Version;
2-
use neo4rs::{config, ConfigBuilder, Graph};
2+
use neo4rs::{ConfigBuilder, Graph};
33
use testcontainers::{clients::Cli, core::WaitFor, Container, Image};
44

55
use std::{collections::HashMap, sync::Arc};
@@ -18,7 +18,7 @@ impl Neo4jContainer {
1818

1919
#[allow(dead_code)]
2020
pub async fn from_version(version: impl Into<String>) -> Self {
21-
Self::from_config_and_version(config(), version).await
21+
Self::from_config_and_version(ConfigBuilder::default(), version).await
2222
}
2323

2424
#[allow(dead_code)]

lib/tests/streams_within_a_transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod container;
55

66
#[tokio::test]
77
async fn streams_within_a_transaction() {
8-
let config = config().fetch_size(1);
8+
let config = ConfigBuilder::default().fetch_size(1);
99
let neo4j = container::Neo4jContainer::from_config(config).await;
1010
let graph = neo4j.graph();
1111

0 commit comments

Comments
 (0)