kxio 3.0.0

Provides injectable Filesystem and Network resources to make code more testable
Documentation
//
use crate::fs::{DirItem, DirItemIterator, Result};

use super::{DirHandle, Error, PathHandle, PathMarker};

impl DirHandle {
    /// Creates a new, empty directory at the path
    ///
    /// Wrapper for [std::fs::create_dir]
    ///
    /// ```
    /// # fn try_main() -> kxio::fs::Result<()> {
    /// let fs = kxio::fs::temp()?;
    /// let path = fs.base().join("foo");
    /// let dir = fs.dir(&path);
    /// dir.create()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn create(&self) -> Result<()> {
        self.check_error()?;
        std::fs::create_dir(self.as_pathbuf()).map_err(Error::Io)
    }

    /// Recursively create a directory and all of its parent components if they are missing.
    ///
    /// Wrapper for [std::fs::create_dir_all]
    ///
    /// ```
    /// # fn try_main() -> kxio::fs::Result<()> {
    /// let fs = kxio::fs::temp()?;
    /// let path = fs.base().join("foo").join("bar");
    /// let dir = fs.dir(&path);
    /// dir.create_all()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn create_all(&self) -> Result<()> {
        self.check_error()?;
        std::fs::create_dir_all(self.as_pathbuf()).map_err(Error::Io)
    }

    /// Returns an iterator over the entries within a directory.
    ///
    /// Wrapper for [std::fs::read_dir]
    ///
    /// ```
    /// # fn try_main() -> kxio::fs::Result<()> {
    /// let fs = kxio::fs::temp()?;
    /// let path = fs.base().join("foo").join("bar");
    /// let dir = fs.dir(&path);
    /// for entry in dir.read()? { /* ... */ }
    /// # Ok(())
    /// # }
    /// ```
    pub fn read(&self) -> Result<Box<dyn Iterator<Item = Result<DirItem>>>> {
        self.check_error()?;
        let read_dir = std::fs::read_dir(self.as_pathbuf()).map_err(Error::Io)?;
        Ok(Box::new(DirItemIterator::new(read_dir)))
    }

    /// Removes an empty directory.
    ///
    /// Wrapper for [std::fs::remove_dir]
    ///
    /// ```
    /// # fn try_main() -> kxio::fs::Result<()> {
    /// let fs = kxio::fs::temp()?;
    /// let path = fs.base().join("foo").join("bar");
    /// let dir = fs.dir(&path);
    /// dir.remove()?;
    /// # Ok(())
    /// }
    pub fn remove(&self) -> Result<()> {
        self.check_error()?;
        std::fs::remove_dir(self.as_pathbuf()).map_err(Error::Io)
    }

    /// Recursively remove a directory and all of its contents.
    ///
    /// Wrapper for [std::fs::remove_dir_all]
    ///
    /// ```
    /// # fn try_main() -> kxio::fs::Result<()> {
    /// let fs = kxio::fs::temp()?;
    /// let path = fs.base().join("foo").join("bar");
    /// let dir = fs.dir(&path);
    /// dir.remove_all()?;
    /// # Ok(())
    /// }
    pub fn remove_all(&self) -> Result<()> {
        self.check_error()?;
        std::fs::remove_dir_all(self.as_pathbuf()).map_err(Error::Io)
    }
}
impl TryFrom<PathHandle<PathMarker>> for DirHandle {
    type Error = crate::fs::Error;

    fn try_from(path: PathHandle<PathMarker>) -> std::result::Result<Self, Self::Error> {
        match path.as_dir() {
            Ok(Some(dir)) => Ok(dir.clone()),
            Ok(None) => Err(crate::fs::Error::NotADirectory {
                path: path.as_pathbuf(),
            }),
            Err(err) => Err(err),
        }
    }
}