pub struct GenericDir { /* private fields */ }Expand description
Generic directory type for type-erased directory operations.
This type can be used when you need to work with directories in a generic way, without knowing their specific type at compile time. It supports conversion to and from any typed directory struct.
§Examples
use tree_type::{dir_type, GenericDir};
dir_type!(CacheDir);
let cache = CacheDir::new("/var/cache")?;
let generic: GenericDir = cache.into();
let cache_again = CacheDir::from_generic(generic);Implementations§
Source§impl GenericDir
impl GenericDir
Sourcepub fn new(path: impl Into<PathBuf>) -> Result<Self>
pub fn new(path: impl Into<PathBuf>) -> Result<Self>
Create a new wrapper for a directory
§Errors
If the directory is invalid. Invalid directory includes being empty.
pub fn as_path(&self) -> &Path
pub fn exists(&self) -> bool
Sourcepub fn as_generic(&self) -> GenericDir
pub fn as_generic(&self) -> GenericDir
Returns a clone of the GenericDir
Sourcepub fn create(&self) -> Result<()>
pub fn create(&self) -> Result<()>
Creates a new, empty directory at the provided path
§Errors
May return any of the same errors as std::fs::create_dir
Sourcepub fn create_all(&self) -> Result<()>
pub fn create_all(&self) -> Result<()>
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
Sourcepub fn remove_all(&self) -> Result<()>
pub fn remove_all(&self) -> Result<()>
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
Sourcepub fn read_dir(&self) -> Result<impl Iterator<Item = Result<GenericPath>>>
pub fn read_dir(&self) -> Result<impl Iterator<Item = Result<GenericPath>>>
Returns an iterator over the entries within a directory.
§Errors
May return any of the same errors as std::fs::read_dir
Sourcepub fn fs_metadata(&self) -> Result<Metadata>
pub fn fs_metadata(&self) -> Result<Metadata>
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
Sourcepub fn file_name(&self) -> String
pub fn file_name(&self) -> String
Returns the final component of the path as a String.
For root paths like “/”, returns an empty string.
See std::path::Path::file_name for more details.
Sourcepub fn rename(self, new_path: impl AsRef<Path>) -> Result<Self, (Error, Self)>
pub fn rename(self, new_path: impl AsRef<Path>) -> Result<Self, (Error, Self)>
Renames this directory to a new name, replacing the original item if
to already exists.
Consumes the original instance and returns a new instance with the updated path on success. On failure, returns both the error and the original instance for recovery. Parent directories are created automatically if they don’t exist.
§Examples
use tree_type::GenericDir;
let dir = GenericDir::new("old_dir")?;
match dir.rename("new_dir") {
Ok(renamed_dir) => {
// Use renamed_dir
}
Err((error, original_dir)) => {
// Handle error, original_dir is still available
}
}§Errors
May return any of the same errors as std::fs::rename
Sourcepub fn parent(&self) -> Option<GenericDir>
pub fn parent(&self) -> Option<GenericDir>
Get the parent directory as GenericDir.
Returns None for directories at the root level.
Trait Implementations§
Source§impl AsRef<Path> for GenericDir
impl AsRef<Path> for GenericDir
Source§impl Clone for GenericDir
impl Clone for GenericDir
Source§fn clone(&self) -> GenericDir
fn clone(&self) -> GenericDir
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more