Skip to content

Commit a71baed

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

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

ip2country/src/lib.rs

+31-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,18 @@ 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+
///
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)?;
97107
Ok(self)
98108
}
99109

@@ -104,7 +114,18 @@ impl AsnDB {
104114
/// Will return `Err` if `file` does not exist or the user does not have
105115
/// permission to read it, or when the content was not in the correct format
106116
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)?;
108129
Ok(self)
109130
}
110131

@@ -179,16 +200,16 @@ impl AsnDB {
179200
self.lookup(ip).and_then(code_to_str)
180201
}
181202

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>>
183204
where
184-
P: AsRef<Path>,
205+
R: std::io::Read,
185206
{
186-
let file = File::open(filename)?;
187-
Ok(io::BufReader::new(file).lines())
207+
io::BufReader::new(reader).lines()
188208
}
189209

190-
fn load_file<T>(file: &str) -> Result<Vec<Asn<T>>>
210+
fn from_reader<T, R>(reader: R) -> Result<Vec<Asn<T>>>
191211
where
212+
R: std::io::Read,
192213
T: FromStr<Err = ParseIntError>
193214
+ From<u32>
194215
+ PartialEq
@@ -198,7 +219,7 @@ impl AsnDB {
198219
{
199220
let mut entries = Vec::new();
200221

201-
let lines = Self::read_lines(file)?;
222+
let lines = Self::read_lines(reader);
202223

203224
let mut last_end = None;
204225
for line in lines {
@@ -250,7 +271,7 @@ mod test {
250271

251272
#[test]
252273
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();
254275

255276
assert_eq!(db.len(), 78);
256277
}

0 commit comments

Comments
 (0)