Skip to content

Commit

Permalink
Revert "📝 Support loading the CSV file from a buffer (#4)"
Browse files Browse the repository at this point in the history
This reverts commit 35edd05.
  • Loading branch information
extrawurst committed Dec 3, 2024
1 parent 35edd05 commit 35e5f75
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions ip2country/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr},
num::ParseIntError,
ops::Add,
path::Path,
str::FromStr,
};

Expand Down Expand Up @@ -92,14 +93,7 @@ impl AsnDB {
/// Will return `Err` if `file` does not exist or the user does not have
/// permission to read it, or when the content was not in the correct format
pub fn load_ipv4(mut self, file: &str) -> Result<Self> {
self.ip_db_v4 = Self::from_reader(File::open(file)?)?;
Ok(self)
}

/// loads csv file of format: ip-range-start (v4),ip-range-end,short-country-code
/// from a reader
pub fn load_ipv4_from_reader<R: std::io::Read>(mut self, reader: R) -> Result<Self> {
self.ip_db_v4 = Self::from_reader(reader)?;
self.ip_db_v4 = Self::load_file(file)?;
Ok(self)
}

Expand All @@ -110,14 +104,7 @@ impl AsnDB {
/// Will return `Err` if `file` does not exist or the user does not have
/// permission to read it, or when the content was not in the correct format
pub fn load_ipv6(mut self, file: &str) -> Result<Self> {
self.ip_db_v6 = Self::from_reader(File::open(file)?)?;
Ok(self)
}

/// loads csv file of format: ip-range-start (v4),ip-range-end,short-country-code
/// from a reader
pub fn load_ipv6_from_reader<R: std::io::Read>(mut self, reader: R) -> Result<Self> {
self.ip_db_v6 = Self::from_reader(reader)?;
self.ip_db_v6 = Self::load_file(file)?;
Ok(self)
}

Expand Down Expand Up @@ -192,16 +179,16 @@ impl AsnDB {
self.lookup(ip).and_then(code_to_str)
}

fn read_lines<R>(reader: R) -> io::Result<io::Lines<io::BufReader<R>>>
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
R: std::io::Read,
P: AsRef<Path>,
{
Ok(io::BufReader::new(reader).lines())
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

fn from_reader<T, R>(reader: R) -> Result<Vec<Asn<T>>>
fn load_file<T>(file: &str) -> Result<Vec<Asn<T>>>
where
R: std::io::Read,
T: FromStr<Err = ParseIntError>
+ From<u32>
+ PartialEq
Expand All @@ -211,7 +198,7 @@ impl AsnDB {
{
let mut entries = Vec::new();

let lines = Self::read_lines(reader)?;
let lines = Self::read_lines(file)?;

let mut last_end = None;
for line in lines {
Expand Down Expand Up @@ -263,7 +250,7 @@ mod test {

#[test]
fn test_load_ipv4() {
let db = AsnDB::from_reader::<u32>(File::open("test/example.csv").unwrap()).unwrap();
let db = AsnDB::load_file::<u32>("test/example.csv").unwrap();

assert_eq!(db.len(), 78);
}
Expand Down

0 comments on commit 35e5f75

Please sign in to comment.