use std::path::PathBuf;
use log::{debug, trace};
use crate::{
error::{FsError, FsResult},
io::FsIo,
};
#[derive(Debug)]
pub struct CreateFile {
contents: Option<(PathBuf, Vec<u8>)>,
}
impl CreateFile {
pub fn new(path: impl Into<PathBuf>, contents: impl IntoIterator<Item = u8>) -> Self {
let contents = contents.into_iter().collect();
let contents = Some((path.into(), contents));
Self { contents }
}
pub fn resume(&mut self, arg: Option<FsIo>) -> FsResult {
let Some(arg) = arg else {
let Some((path, contents)) = self.contents.take() else {
return FsResult::Err(FsError::MissingInput);
};
trace!("wants I/O to create file at {}", path.display());
return FsResult::Io(FsIo::CreateFile(Err((path, contents))));
};
debug!("resume after creating file");
let FsIo::CreateFile(io) = arg else {
let err = FsError::InvalidArgument("create file output", arg);
return FsResult::Err(err);
};
match io {
Ok(()) => FsResult::Ok(()),
Err(path) => FsResult::Io(FsIo::CreateFile(Err(path))),
}
}
}