tree-type 0.4.5

Rust macros for creating type-safe filesystem tree structures
Documentation
//! Filesystem operations with feature-gated error handling

use std::path::Path;
use std::path::PathBuf;

// Re-export types for generated code
#[cfg(feature = "enhanced-errors")]
pub use crate::deps::enhanced_errors::ReadDir;

#[cfg(not(feature = "enhanced-errors"))]
pub use std::fs::ReadDir;

// Metadata is the same for both `fs_err` and `std::fs`
pub use std::fs::Metadata;

/// Create an empty file if it doesn't already exist
///
/// # Errors
///  
/// May return any of the same errors as `std::fs::open`
pub fn create_file<P: Into<PathBuf> + std::convert::AsRef<std::path::Path>>(
    path: P,
) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    crate::deps::enhanced_errors::File::create(path)?;

    #[cfg(not(feature = "enhanced-errors"))]
    ::std::fs::File::create(path)?;

    Ok(())
}

/// Renames a file or directory to a new name, replacing the original file if
/// `to` already exists.
///
/// # Errors
///  
/// May return any of the same errors as `std::fs::rename`
pub fn rename<From: AsRef<Path>, To: AsRef<Path>>(from: From, to: To) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    crate::deps::enhanced_errors::rename(from, to)?;

    #[cfg(not(feature = "enhanced-errors"))]
    ::std::fs::rename(from, to)?;

    Ok(())
}

/// Changes the permissions found on a file or a directory.
///
/// # Errors
///  
/// May return any of the same errors as `std::fs::set_permissions`
pub fn set_permissions<P: AsRef<Path>>(
    path: P,
    perms: std::fs::Permissions,
) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    crate::deps::enhanced_errors::set_permissions(path, perms)?;

    #[cfg(not(feature = "enhanced-errors"))]
    std::fs::set_permissions(path, perms)?;

    Ok(())
}

/// Creates a new, empty directory at the provided path
///
/// # Errors
///  
/// May return any of the same errors as `std::fs::create_dir`
pub fn create_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::create_dir(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::create_dir(path)
    }
}

/// Recursively create a directory and all of its parent components if they are missing.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::create_dir_all`
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::create_dir_all(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::create_dir_all(path)
    }
}

/// Removes an empty directory.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::remove_all`
pub fn remove_dir<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::remove_dir(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::remove_dir(path)
    }
}

/// Removes a directory at this path, after removing all its contents. Use carefully!
///
/// # Errors
///
/// May return any of the same errors as `std::fs::remove_dir_all`
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::remove_dir_all(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::remove_dir_all(path)
    }
}

/// Returns an iterator over the entries within a directory.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::read_dir`
#[cfg(feature = "enhanced-errors")]
pub fn read_dir<P: AsRef<Path>>(path: P) -> std::io::Result<crate::deps::enhanced_errors::ReadDir> {
    crate::deps::enhanced_errors::read_dir(path.as_ref())
}

/// Returns an iterator over the entries within a directory.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::read_dir`
#[cfg(not(feature = "enhanced-errors"))]
pub fn read_dir<P: AsRef<Path>>(path: P) -> std::io::Result<std::fs::ReadDir> {
    std::fs::read_dir(path)
}

/// Given a path, queries the file system to get information about a file, directory, etc.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::metadata`
pub fn metadata<P: AsRef<Path>>(path: P) -> std::io::Result<std::fs::Metadata> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::metadata(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::metadata(path)
    }
}

/// Reads the entire contents of a file into a bytes vector.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::read`
pub fn read<P: AsRef<Path>>(path: P) -> std::io::Result<Vec<u8>> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::read(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::read(path)
    }
}

/// Reads the entire contents of a file into a string.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::read_to_string`
pub fn read_to_string<P: AsRef<Path>>(path: P) -> std::io::Result<String> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::read_to_string(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::read_to_string(path)
    }
}

/// Writes a slice as the entire contents of a file.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::write`
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::write(path, contents)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::write(path, contents)
    }
}

/// Removes a file from the filesystem.
///
/// # Errors
///
/// May return any of the same errors as `std::fs::remove_file`
pub fn remove_file<P: AsRef<Path>>(path: P) -> std::io::Result<()> {
    #[cfg(feature = "enhanced-errors")]
    {
        crate::deps::enhanced_errors::remove_file(path)
    }
    #[cfg(not(feature = "enhanced-errors"))]
    {
        std::fs::remove_file(path)
    }
}