1
1
pub use crate :: errors:: * ;
2
2
3
+ const DEFAULT_DATABASE : & str = "neo4j" ;
3
4
const DEFAULT_FETCH_SIZE : usize = 200 ;
4
5
const DEFAULT_MAX_CONNECTIONS : usize = 16 ;
5
6
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`].
7
8
#[ derive( Debug , Clone ) ]
8
9
pub struct Config {
9
10
pub ( crate ) uri : String ,
@@ -14,97 +15,101 @@ pub struct Config {
14
15
pub ( crate ) fetch_size : usize ,
15
16
}
16
17
17
- /// A builder to override default configurations and build the [`Config`]
18
+ /// A builder to override default configurations and build the [`Config`].
18
19
pub struct ConfigBuilder {
19
20
uri : Option < String > ,
20
21
user : Option < String > ,
21
22
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 ,
25
26
}
26
27
27
28
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 ( ) ) ;
31
37
self
32
38
}
33
39
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 ( ) ) ;
37
43
self
38
44
}
39
45
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 ( ) ) ;
43
49
self
44
50
}
45
51
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 ( ) ;
49
57
self
50
58
}
51
59
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.
55
64
pub fn fetch_size ( mut self , fetch_size : usize ) -> Self {
56
- self . fetch_size = Some ( fetch_size) ;
65
+ self . fetch_size = fetch_size;
57
66
self
58
67
}
59
68
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.
61
72
pub fn max_connections ( mut self , max_connections : usize ) -> Self {
62
- self . max_connections = Some ( max_connections) ;
73
+ self . max_connections = max_connections;
63
74
self
64
75
}
65
76
66
77
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 ) {
77
79
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 ,
84
86
} )
87
+ } else {
88
+ Err ( Error :: InvalidConfig )
85
89
}
86
90
}
87
91
}
88
92
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
+ }
98
103
}
99
104
}
100
105
101
106
#[ cfg( test) ]
102
107
mod tests {
103
108
use super :: * ;
104
109
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 ( )
108
113
. uri ( "127.0.0.1:7687" )
109
114
. user ( "some_user" )
110
115
. password ( "some_password" )
@@ -121,9 +126,9 @@ mod tests {
121
126
assert_eq ! ( config. max_connections, 5 ) ;
122
127
}
123
128
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 ( )
127
132
. uri ( "127.0.0.1:7687" )
128
133
. user ( "some_user" )
129
134
. password ( "some_password" )
@@ -132,26 +137,26 @@ mod tests {
132
137
assert_eq ! ( config. uri, "127.0.0.1:7687" ) ;
133
138
assert_eq ! ( config. user, "some_user" ) ;
134
139
assert_eq ! ( config. password, "some_password" ) ;
135
- assert_eq ! ( config. db, "" ) ;
140
+ assert_eq ! ( config. db, "neo4j " ) ;
136
141
assert_eq ! ( config. fetch_size, 200 ) ;
137
142
assert_eq ! ( config. max_connections, 16 ) ;
138
143
}
139
144
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 ( )
143
148
. user( "some_user" )
144
149
. password( "some_password" )
145
150
. build( )
146
151
. is_err( ) ) ;
147
152
148
- assert ! ( config ( )
153
+ assert ! ( ConfigBuilder :: default ( )
149
154
. uri( "127.0.0.1:7687" )
150
155
. password( "some_password" )
151
156
. build( )
152
157
. is_err( ) ) ;
153
158
154
- assert ! ( config ( )
159
+ assert ! ( ConfigBuilder :: default ( )
155
160
. uri( "127.0.0.1:7687" )
156
161
. user( "some_user" )
157
162
. build( )
0 commit comments