#![warn(missing_docs)]
use crate::ds::DataSource;
use std::path::PathBuf;
#[derive(Debug)]
pub struct CSVDataSource {
path: PathBuf,
}
impl DataSource for CSVDataSource {
fn srid(&self) -> Option<u32> {
None
}
}
impl CSVDataSource {
pub fn from(s: &str) -> Self {
Self { path: s.into() }
}
pub fn path(&self) -> &PathBuf {
&self.path
}
}
#[macro_export]
macro_rules! gen_csv_ds {
($vis:vis, $name:expr, $path:expr, $feature:expr) => {
::paste::paste! {
#[derive(Debug)]
$vis struct [<$name CSV>](CSVDataSource);
impl [<$name CSV>] {
$vis fn new() -> Self {
Self(CSVDataSource::from($path))
}
$vis fn reader(&self) -> Result<::csv::Reader<::std::fs::File>, MyError> {
let file = ::std::fs::File::open(self.0.path())?;
Ok(::csv::Reader::from_reader(file))
}
}
impl ::core::fmt::Display for [<$name CSV>] {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "{}CSV({})", $name, $path)
}
}
impl IterableDS for [<$name CSV>] {
type Item = $feature;
type Err = MyError;
fn iter(&self) -> Result<impl Iterator<Item = Result<$feature, Self::Err>>, Self::Err> {
let file = ::std::fs::File::open(&self.0.path())?;
let rdr = ::csv::Reader::from_reader(file);
let it = rdr.into_deserialize().map(|res| res.map_err(MyError::from));
Ok(it)
}
}
}
}
}