nimbus/stateful/client/
fs_client.rsuse crate::error::Result;
use crate::stateful::client::SettingsClient;
use crate::Experiment;
use std::ffi::OsStr;
use std::fs::File;
use std::io::BufReader;
use std::path::{Path, PathBuf};
pub struct FileSystemClient {
path: PathBuf,
}
impl FileSystemClient {
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(Self {
path: path.as_ref().into(),
})
}
}
impl SettingsClient for FileSystemClient {
fn get_experiments_metadata(&self) -> Result<String> {
unimplemented!();
}
fn fetch_experiments(&self) -> Result<Vec<Experiment>> {
log::info!("reading experiments in {}", self.path.display());
let mut res = Vec::new();
let json_ext = Some(OsStr::new("json"));
let filenames = self
.path
.read_dir()?
.filter_map(Result::ok)
.map(|c| c.path())
.filter(|f| f.is_file() && f.extension() == json_ext);
for child_path in filenames {
let file = File::open(child_path.clone())?;
let reader = BufReader::new(file);
match serde_json::from_reader::<_, Experiment>(reader) {
Ok(exp) => res.push(exp),
Err(e) => {
log::warn!(
"Malformed experiment found! File {}, Error: {}",
child_path.display(),
e
);
}
}
}
Ok(res)
}
}