@@ -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,14 @@ 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
+ pub fn load_ipv4_from_reader < R : std:: io:: Read > ( mut self , reader : R ) -> Result < Self > {
102
+ self . ip_db_v4 = Self :: from_reader ( reader) ?;
97
103
Ok ( self )
98
104
}
99
105
@@ -104,7 +110,14 @@ impl AsnDB {
104
110
/// Will return `Err` if `file` does not exist or the user does not have
105
111
/// permission to read it, or when the content was not in the correct format
106
112
pub fn load_ipv6 ( mut self , file : & str ) -> Result < Self > {
107
- self . ip_db_v6 = Self :: load_file ( file) ?;
113
+ self . ip_db_v6 = Self :: from_reader ( File :: open ( file) ?) ?;
114
+ Ok ( self )
115
+ }
116
+
117
+ /// loads csv file of format: ip-range-start (v4),ip-range-end,short-country-code
118
+ /// from a reader
119
+ pub fn load_ipv6_from_reader < R : std:: io:: Read > ( mut self , reader : R ) -> Result < Self > {
120
+ self . ip_db_v6 = Self :: from_reader ( reader) ?;
108
121
Ok ( self )
109
122
}
110
123
@@ -179,16 +192,16 @@ impl AsnDB {
179
192
self . lookup ( ip) . and_then ( code_to_str)
180
193
}
181
194
182
- fn read_lines < P > ( filename : P ) -> io:: Result < io:: Lines < io:: BufReader < File > > >
195
+ fn read_lines < R > ( reader : R ) -> io:: Result < io:: Lines < io:: BufReader < R > > >
183
196
where
184
- P : AsRef < Path > ,
197
+ R : std :: io :: Read ,
185
198
{
186
- let file = File :: open ( filename) ?;
187
- Ok ( io:: BufReader :: new ( file) . lines ( ) )
199
+ Ok ( io:: BufReader :: new ( reader) . lines ( ) )
188
200
}
189
201
190
- fn load_file < T > ( file : & str ) -> Result < Vec < Asn < T > > >
202
+ fn from_reader < T , R > ( reader : R ) -> Result < Vec < Asn < T > > >
191
203
where
204
+ R : std:: io:: Read ,
192
205
T : FromStr < Err = ParseIntError >
193
206
+ From < u32 >
194
207
+ PartialEq
@@ -198,7 +211,7 @@ impl AsnDB {
198
211
{
199
212
let mut entries = Vec :: new ( ) ;
200
213
201
- let lines = Self :: read_lines ( file ) ?;
214
+ let lines = Self :: read_lines ( reader ) ?;
202
215
203
216
let mut last_end = None ;
204
217
for line in lines {
@@ -250,7 +263,7 @@ mod test {
250
263
251
264
#[ test]
252
265
fn test_load_ipv4 ( ) {
253
- let db = AsnDB :: load_file :: < u32 > ( "test/example.csv" ) . unwrap ( ) ;
266
+ let db = AsnDB :: from_reader :: < u32 > ( File :: open ( "test/example.csv" ) . unwrap ( ) ) . unwrap ( ) ;
254
267
255
268
assert_eq ! ( db. len( ) , 78 ) ;
256
269
}
0 commit comments