pub mod ban;
pub mod bano;
pub mod be;
pub mod grouped;
pub mod lu;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use flate2::read::MultiGzDecoder;
use crate::doc::Document;
use crate::error::GeocoderError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Source {
Addok,
Bano,
Lu,
Be,
}
impl Source {
pub fn from_name(name: &str) -> Option<Self> {
match name {
"addok" | "ban" => Some(Self::Addok),
"bano" => Some(Self::Bano),
"lu" | "bd-adresses" => Some(Self::Lu),
"be" | "best" => Some(Self::Be),
_ => None,
}
}
pub fn name(self) -> &'static str {
match self {
Self::Addok => "addok",
Self::Bano => "bano",
Self::Lu => "lu",
Self::Be => "be",
}
}
pub fn is_line_based(self) -> bool {
matches!(self, Self::Addok | Self::Bano)
}
pub fn parse(self, line: &str) -> Result<Option<Document>, GeocoderError> {
if line.trim().is_empty() {
return Ok(None);
}
match self {
Source::Addok => ban::parse_line(line).map(Some),
Source::Bano => bano::parse_line(line).map(Some),
Source::Lu | Source::Be => Err(GeocoderError::InvalidIndex(
"a register is not read line by line",
)),
}
}
}
pub fn for_each_input(
path: &Path,
mut f: impl FnMut(&mut dyn Read) -> Result<(), GeocoderError>,
) -> Result<(), GeocoderError> {
let name = path.to_string_lossy().to_lowercase();
let file = File::open(path)?;
if name.ends_with(".zip") {
let mut archive = zip::ZipArchive::new(file).map_err(zip_error)?;
for i in 0..archive.len() {
let entry = archive.by_index(i).map_err(zip_error)?;
if entry.is_dir() {
continue;
}
f(&mut BufReader::with_capacity(1 << 20, entry))?;
}
Ok(())
} else if name.ends_with(".gz") {
f(&mut BufReader::with_capacity(
1 << 20,
MultiGzDecoder::new(file),
))
} else {
f(&mut BufReader::with_capacity(1 << 20, file))
}
}
fn zip_error(e: zip::result::ZipError) -> GeocoderError {
GeocoderError::Io(std::io::Error::other(e))
}
pub fn skip_bom<R: Read>(reader: R) -> impl Read {
let mut reader = BufReader::new(reader);
let mut bom = [0u8; 3];
let mut read = 0;
while read < 3 {
match reader.read(&mut bom[read..]) {
Ok(0) => break,
Ok(n) => read += n,
Err(_) => break,
}
}
let head = if bom[..read] == [0xEF, 0xBB, 0xBF] {
Vec::new()
} else {
bom[..read].to_vec()
};
std::io::Cursor::new(head).chain(reader)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skips_the_bom_only() {
let mut out = String::new();
skip_bom("\u{feff}a;b".as_bytes())
.read_to_string(&mut out)
.unwrap();
assert_eq!(out, "a;b");
out.clear();
skip_bom("ab".as_bytes()).read_to_string(&mut out).unwrap();
assert_eq!(out, "ab");
}
}