use std::path::{Path, PathBuf};
use crate::core::io::{ConnectionConf, ConnectionDetails, ConnectionInfo};
use crate::prelude::*;
#[derive(Clone, Debug)]
pub struct FileReader {
pub(crate) path: PathBuf,
pub(crate) info: ConnectionInfo,
}
impl FileReader {
pub fn new(path: impl Into<PathBuf>) -> Result<Self> {
let path: PathBuf = path.into();
if !Path::exists(path.as_path()) {
return Err(Error::from(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("file does not exists: {path:?}"),
)));
}
if !Path::is_file(path.as_path()) {
return Err(Error::from(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("not a file: {path:?}"),
)));
}
let info = ConnectionInfo::new(ConnectionDetails::FileReader { path: path.clone() });
Ok(Self { path, info })
}
}
impl ConnectionConf for FileReader {
fn info(&self) -> &ConnectionInfo {
&self.info
}
}