Skip to content

Commit 2ed4163

Browse files
📝 Support loading the CSV file from a buffer
1 parent b31c18c commit 2ed4163

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

ip2country/src/lib.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::{
1212
net::{IpAddr, Ipv4Addr, Ipv6Addr},
1313
num::ParseIntError,
1414
ops::Add,
15-
path::Path,
1615
str::FromStr,
1716
};
1817

@@ -93,7 +92,14 @@ impl AsnDB {
9392
/// Will return `Err` if `file` does not exist or the user does not have
9493
/// permission to read it, or when the content was not in the correct format
9594
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)?;
97103
Ok(self)
98104
}
99105

@@ -104,7 +110,14 @@ impl AsnDB {
104110
/// Will return `Err` if `file` does not exist or the user does not have
105111
/// permission to read it, or when the content was not in the correct format
106112
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)?;
108121
Ok(self)
109122
}
110123

@@ -179,16 +192,16 @@ impl AsnDB {
179192
self.lookup(ip).and_then(code_to_str)
180193
}
181194

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>>>
183196
where
184-
P: AsRef<Path>,
197+
R: std::io::Read,
185198
{
186-
let file = File::open(filename)?;
187-
Ok(io::BufReader::new(file).lines())
199+
Ok(io::BufReader::new(reader).lines())
188200
}
189201

190-
fn load_file<T>(file: &str) -> Result<Vec<Asn<T>>>
202+
fn from_reader<T, R>(reader: R) -> Result<Vec<Asn<T>>>
191203
where
204+
R: std::io::Read,
192205
T: FromStr<Err = ParseIntError>
193206
+ From<u32>
194207
+ PartialEq
@@ -198,7 +211,7 @@ impl AsnDB {
198211
{
199212
let mut entries = Vec::new();
200213

201-
let lines = Self::read_lines(file)?;
214+
let lines = Self::read_lines(reader)?;
202215

203216
let mut last_end = None;
204217
for line in lines {
@@ -250,7 +263,7 @@ mod test {
250263

251264
#[test]
252265
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();
254267

255268
assert_eq!(db.len(), 78);
256269
}

0 commit comments

Comments
 (0)