use super::StrictPath;
#[must_use = "open_with() returns a builder — chain .read(), .write(), .create(), .open() to use it"]
pub struct StrictOpenOptions<'path, Marker> {
path: &'path StrictPath<Marker>,
options: std::fs::OpenOptions,
}
impl<'path, Marker> StrictOpenOptions<'path, Marker> {
#[inline]
pub(crate) fn new(path: &'path StrictPath<Marker>) -> Self {
Self {
path,
options: std::fs::OpenOptions::new(),
}
}
#[inline]
pub fn read(mut self, read: bool) -> Self {
self.options.read(read);
self
}
#[inline]
pub fn write(mut self, write: bool) -> Self {
self.options.write(write);
self
}
#[inline]
pub fn append(mut self, append: bool) -> Self {
self.options.append(append);
self
}
#[inline]
pub fn truncate(mut self, truncate: bool) -> Self {
self.options.truncate(truncate);
self
}
#[inline]
pub fn create(mut self, create: bool) -> Self {
self.options.create(create);
self
}
#[inline]
pub fn create_new(mut self, create_new: bool) -> Self {
self.options.create_new(create_new);
self
}
#[inline]
pub fn open(self) -> std::io::Result<std::fs::File> {
self.options.open(self.path.path())
}
}
pub struct StrictReadDir<'path, Marker> {
pub(super) inner: std::fs::ReadDir,
pub(super) parent: &'path StrictPath<Marker>,
}
impl<Marker> std::fmt::Debug for StrictReadDir<'_, Marker> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StrictReadDir")
.field("parent", &self.parent.strictpath_display())
.finish_non_exhaustive()
}
}
impl<Marker: Clone> Iterator for StrictReadDir<'_, Marker> {
type Item = std::io::Result<StrictPath<Marker>>;
fn next(&mut self) -> Option<Self::Item> {
match self.inner.next()? {
Ok(entry) => {
let file_name = entry.file_name();
match self.parent.strict_join(file_name) {
Ok(strict_path) => Some(Ok(strict_path)),
Err(e) => Some(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e))),
}
}
Err(e) => Some(Err(e)),
}
}
}