@@ -12,7 +12,6 @@ use std::{
12
12
net:: { IpAddr , Ipv4Addr , Ipv6Addr } ,
13
13
num:: ParseIntError ,
14
14
ops:: Add ,
15
- path:: Path ,
16
15
str:: FromStr ,
17
16
} ;
18
17
@@ -93,7 +92,18 @@ impl AsnDB {
93
92
/// Will return `Err` if `file` does not exist or the user does not have
94
93
/// permission to read it, or when the content was not in the correct format
95
94
pub fn load_ipv4 ( mut self , file : & str ) -> Result < Self > {
96
- self . ip_db_v4 = Self :: load_file ( file) ?;
95
+ self . ip_db_v4 = Self :: from_reader ( File :: open ( file) ?) ?;
96
+ Ok ( self )
97
+ }
98
+
99
+ /// loads csv file of format: ip-range-start (v4),ip-range-end,short-country-code
100
+ /// from a reader
101
+ ///
102
+ /// # Errors
103
+ ///
104
+ /// Will return an error if we fail to parse the input
105
+ pub fn load_ipv4_from_reader < R : std:: io:: Read > ( mut self , reader : R ) -> Result < Self > {
106
+ self . ip_db_v4 = Self :: from_reader ( reader) ?;
97
107
Ok ( self )
98
108
}
99
109
@@ -104,7 +114,18 @@ impl AsnDB {
104
114
/// Will return `Err` if `file` does not exist or the user does not have
105
115
/// permission to read it, or when the content was not in the correct format
106
116
pub fn load_ipv6 ( mut self , file : & str ) -> Result < Self > {
107
- self . ip_db_v6 = Self :: load_file ( file) ?;
117
+ self . ip_db_v6 = Self :: from_reader ( File :: open ( file) ?) ?;
118
+ Ok ( self )
119
+ }
120
+
121
+ /// loads csv file of format: ip-range-start (v4),ip-range-end,short-country-code
122
+ /// from a reader
123
+ ///
124
+ /// # Errors
125
+ ///
126
+ /// Will return an error if we fail to parse the input
127
+ pub fn load_ipv6_from_reader < R : std:: io:: Read > ( mut self , reader : R ) -> Result < Self > {
128
+ self . ip_db_v6 = Self :: from_reader ( reader) ?;
108
129
Ok ( self )
109
130
}
110
131
@@ -179,16 +200,16 @@ impl AsnDB {
179
200
self . lookup ( ip) . and_then ( code_to_str)
180
201
}
181
202
182
- fn read_lines < P > ( filename : P ) -> io:: Result < io :: Lines < io:: BufReader < File > > >
203
+ fn read_lines < R > ( reader : R ) -> io:: Lines < io:: BufReader < R > >
183
204
where
184
- P : AsRef < Path > ,
205
+ R : std :: io :: Read ,
185
206
{
186
- let file = File :: open ( filename) ?;
187
- Ok ( io:: BufReader :: new ( file) . lines ( ) )
207
+ io:: BufReader :: new ( reader) . lines ( )
188
208
}
189
209
190
- fn load_file < T > ( file : & str ) -> Result < Vec < Asn < T > > >
210
+ fn from_reader < T , R > ( reader : R ) -> Result < Vec < Asn < T > > >
191
211
where
212
+ R : std:: io:: Read ,
192
213
T : FromStr < Err = ParseIntError >
193
214
+ From < u32 >
194
215
+ PartialEq
@@ -198,7 +219,7 @@ impl AsnDB {
198
219
{
199
220
let mut entries = Vec :: new ( ) ;
200
221
201
- let lines = Self :: read_lines ( file ) ? ;
222
+ let lines = Self :: read_lines ( reader ) ;
202
223
203
224
let mut last_end = None ;
204
225
for line in lines {
@@ -250,7 +271,7 @@ mod test {
250
271
251
272
#[ test]
252
273
fn test_load_ipv4 ( ) {
253
- let db = AsnDB :: load_file :: < u32 > ( "test/example.csv" ) . unwrap ( ) ;
274
+ let db = AsnDB :: from_reader :: < u32 > ( File :: open ( "test/example.csv" ) . unwrap ( ) ) . unwrap ( ) ;
254
275
255
276
assert_eq ! ( db. len( ) , 78 ) ;
256
277
}
0 commit comments