pub struct GenericFile { /* private fields */ }Implementations§
Source§impl GenericFile
impl GenericFile
Sourcepub fn new(path: impl Into<PathBuf>) -> Result<Self>
pub fn new(path: impl Into<PathBuf>) -> Result<Self>
Create a new wrapper for a file
§Errors
If the file name is invalid. Invalid file name inclued empty file name.
pub fn as_path(&self) -> &Path
pub fn exists(&self) -> bool
pub fn as_generic(&self) -> GenericFile
Sourcepub fn read(&self) -> Result<Vec<u8>>
pub fn read(&self) -> Result<Vec<u8>>
Reads the entire contents of a file into a bytes vector.
§Errors
May return any of the same errors as std::fs::read
Sourcepub fn read_to_string(&self) -> Result<String>
pub fn read_to_string(&self) -> Result<String>
Reads the entire contents of a file into a string.
§Errors
May return any of the same errors as std::fs::read_to_string
Sourcepub fn write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()>
pub fn write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()>
Writes a slice as the entire contents of a file.
§Errors
May return any of the same errors as std::fs::write
Sourcepub fn create(&self) -> Result<()>
pub fn create(&self) -> Result<()>
Create an empty file if it doesn’t already exist
§Errors
May return any of the same errors as std::fs::open
Sourcepub fn remove(&self) -> Result<()>
pub fn remove(&self) -> Result<()>
Removes a file from the filesystem.
§Errors
May return any of the same errors as std::fs::remove_file
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.
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)>
Rename/move this file to a new path.
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::GenericFile;
let file = GenericFile::new("old.txt")?;
match file.rename("new.txt") {
Ok(renamed_file) => {
// Use renamed_file
}
Err((error, original_file)) => {
// Handle error, original_file is still available
}
}§Errors
May return any of the same errors as std::fs::rename
Sourcepub fn parent(&self) -> GenericDir
pub fn parent(&self) -> GenericDir
Get the parent directory as GenericDir.
Files always have a parent directory.
Trait Implementations§
Source§impl AsRef<Path> for GenericFile
impl AsRef<Path> for GenericFile
Source§impl Clone for GenericFile
impl Clone for GenericFile
Source§fn clone(&self) -> GenericFile
fn clone(&self) -> GenericFile
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more