1
- use std:: io:: { Write , Read } ;
1
+ use std:: io:: { Read , Write } ;
2
2
// Structs
3
3
4
4
///
5
5
/// Configuration for our application
6
- ///
6
+ ///
7
7
pub struct Config {
8
- values : Vec < ( String , String ) >
8
+ values : Vec < ( String , String ) > ,
9
9
}
10
10
11
11
///
12
12
/// A service for managing a configuration
13
- ///
14
- pub struct KeyValueConfigService {
15
-
16
- }
13
+ ///
14
+ pub struct KeyValueConfigService { }
17
15
18
16
// Traits
19
17
20
18
///
21
- /// Provides a get() function to return valuse associated with
19
+ /// Provides a get() function to return values associated with
22
20
/// the specified key.
23
- ///
21
+ ///
24
22
pub trait ValueGetter {
25
23
fn get ( & self , s : & str ) -> Option < String > ;
26
24
}
27
25
28
26
///
29
27
/// Write a config
30
- ///
28
+ ///
31
29
pub trait ConfigWriter {
32
30
fn write ( & self , config : Config , to : & mut impl Write ) -> std:: io:: Result < ( ) > ;
33
31
}
34
32
35
33
///
36
34
/// Read a config
37
- ///
35
+ ///
38
36
pub trait ConfigReader {
39
37
fn read ( & self , from : & mut impl Read ) -> std:: io:: Result < Config > ;
40
38
}
@@ -43,16 +41,13 @@ pub trait ConfigReader {
43
41
44
42
impl Config {
45
43
pub fn new ( values : Vec < ( String , String ) > ) -> Config {
46
- Config {
47
- values : values
48
- }
44
+ Config { values : values }
49
45
}
50
46
}
51
47
52
48
impl KeyValueConfigService {
53
49
pub fn new ( ) -> KeyValueConfigService {
54
- KeyValueConfigService {
55
- }
50
+ KeyValueConfigService { }
56
51
}
57
52
}
58
53
@@ -72,34 +67,35 @@ impl ConfigReader for KeyValueConfigService {
72
67
73
68
// chain iterators together and collect the results
74
69
let values: Vec < ( String , String ) > = buffer
75
- . split_terminator ( "\n " ) // split
70
+ . split_terminator ( "\n " ) // split
76
71
. map ( |line| line. trim ( ) ) // remove whitespace
77
- . filter ( |line| { // filter invalid lines
78
- let pos = line . find ( "=" )
79
- . unwrap_or ( 0 ) ;
72
+ . filter ( |line| {
73
+ // filter invalid lines
74
+ let pos = line . find ( "=" ) . unwrap_or ( 0 ) ;
80
75
pos > 0 && pos < line. len ( ) - 1
81
76
} )
82
- . map ( |line| { // create a tuple from a line
77
+ . map ( |line| {
78
+ // create a tuple from a line
83
79
let parts = line. split ( "=" ) . collect :: < Vec < & str > > ( ) ;
84
80
( parts[ 0 ] . to_string ( ) , parts[ 1 ] . to_string ( ) )
85
81
} )
86
- . collect ( ) ; // transform it into a vector
82
+ . collect ( ) ; // transform it into a vector
87
83
Ok ( Config :: new ( values) )
88
84
}
89
85
}
90
86
91
87
impl ValueGetter for Config {
92
88
fn get ( & self , s : & str ) -> Option < String > {
93
- self . values . iter ( )
94
- . find_map ( |tuple| if & tuple. 0 == s {
95
- Some ( tuple. 1 . clone ( ) )
96
- } else {
97
- None
98
- } )
89
+ self . values . iter ( ) . find_map ( |tuple| {
90
+ if & tuple. 0 == s {
91
+ Some ( tuple. 1 . clone ( ) )
92
+ } else {
93
+ None
94
+ }
95
+ } )
99
96
}
100
97
}
101
98
102
-
103
99
#[ cfg( test) ]
104
100
mod tests {
105
101
use super :: * ;
@@ -112,7 +108,6 @@ mod tests {
112
108
assert_eq ! ( config. get( "HELLO" ) , None ) ;
113
109
}
114
110
115
-
116
111
#[ test]
117
112
fn keyvalueconfigservice_write_config ( ) {
118
113
let config = Config :: new ( vec ! [ ( "hello" . to_string( ) , "world" . to_string( ) ) ] ) ;
@@ -121,20 +116,27 @@ mod tests {
121
116
let mut target = vec ! [ ] ;
122
117
assert ! ( service. write( config, & mut target) . is_ok( ) ) ;
123
118
124
- assert_eq ! ( String :: from_utf8( target) . unwrap( ) , "hello=world\n " . to_string( ) ) ;
119
+ assert_eq ! (
120
+ String :: from_utf8( target) . unwrap( ) ,
121
+ "hello=world\n " . to_string( )
122
+ ) ;
125
123
}
126
124
127
- #[ test]
125
+ #[ test]
128
126
fn keyvalueconfigservice_read_config ( ) {
129
-
130
127
let service = KeyValueConfigService :: new ( ) ;
131
128
let readable = & format ! ( "{}\n {}" , "hello=world" , "a=b" ) . into_bytes ( ) ;
132
-
133
- let config = service. read ( & mut Cursor :: new ( readable) )
129
+
130
+ let config = service
131
+ . read ( & mut Cursor :: new ( readable) )
134
132
. expect ( "Couldn't read from the vector" ) ;
135
133
136
- assert_eq ! ( config. values, vec![
134
+ assert_eq ! (
135
+ config. values,
136
+ vec![
137
137
( "hello" . to_string( ) , "world" . to_string( ) ) ,
138
- ( "a" . to_string( ) , "b" . to_string( ) ) ] ) ;
138
+ ( "a" . to_string( ) , "b" . to_string( ) )
139
+ ]
140
+ ) ;
139
141
}
140
- }
142
+ }
0 commit comments