pub struct VfsPath { /* private fields */ }
Expand description
A virtual filesystem path, identifying a single file or directory in this virtual filesystem
Implementations
sourceimpl VfsPath
impl VfsPath
sourcepub fn new<T: FileSystem>(filesystem: T) -> Self
pub fn new<T: FileSystem>(filesystem: T) -> Self
Creates a root path for the given filesystem
let path = VfsPath::new(PhysicalFS::new("."));
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the string representation of this path
let path = VfsPath::new(PhysicalFS::new("."));
assert_eq!(path.as_str(), "");
assert_eq!(path.join("foo.txt")?.as_str(), "/foo.txt");
sourcepub fn join(&self, path: impl AsRef<str>) -> VfsResult<Self>
pub fn join(&self, path: impl AsRef<str>) -> VfsResult<Self>
Appends a path segment to this path, returning the result
let path = VfsPath::new(PhysicalFS::new("."));
assert_eq!(path.join("foo.txt")?.as_str(), "/foo.txt");
assert_eq!(path.join("foo/bar.txt")?.as_str(), "/foo/bar.txt");
let foo = path.join("foo")?;
assert_eq!(path.join("foo/bar.txt")?, foo.join("bar.txt")?);
assert_eq!(path, foo.join("..")?);
sourcepub fn root(&self) -> VfsPath
pub fn root(&self) -> VfsPath
Returns the root path of this filesystem
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo/bar")?;
assert_eq!(directory.root(), path);
sourcepub fn create_dir(&self) -> VfsResult<()>
pub fn create_dir(&self) -> VfsResult<()>
Creates the directory at this path
Note that the parent directory must exist.
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir()?;
assert!(directory.exists()?);
assert_eq!(directory.metadata()?.file_type, VfsFileType::Directory);
sourcepub fn create_dir_all(&self) -> VfsResult<()>
pub fn create_dir_all(&self) -> VfsResult<()>
Creates the directory at this path, also creating parent directories as necessary
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo/bar")?;
directory.create_dir_all()?;
assert!(directory.exists()?);
assert_eq!(directory.metadata()?.file_type, VfsFileType::Directory);
let parent = path.join("foo")?;
assert!(parent.exists()?);
assert_eq!(parent.metadata()?.file_type, VfsFileType::Directory);
sourcepub fn read_dir(&self) -> VfsResult<Box<dyn Iterator<Item = VfsPath>>>
pub fn read_dir(&self) -> VfsResult<Box<dyn Iterator<Item = VfsPath>>>
Iterates over all entries of this directory path
let path = VfsPath::new(MemoryFS::new());
path.join("foo")?.create_dir()?;
path.join("bar")?.create_dir()?;
let mut directories: Vec<_> = path.read_dir()?.collect();
directories.sort_by_key(|path| path.as_str().to_string());
assert_eq!(directories, vec![path.join("bar")?, path.join("foo")?]);
sourcepub fn create_file(&self) -> VfsResult<Box<dyn Write>>
pub fn create_file(&self) -> VfsResult<Box<dyn Write>>
Creates a file at this path for writing, overwriting any existing file
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file()?, "Hello, world!")?;
let mut result = String::new();
file.open_file()?.read_to_string(&mut result)?;
assert_eq!(&result, "Hello, world!");
sourcepub fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead>>
pub fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead>>
Opens the file at this path for reading
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file()?, "Hello, world!")?;
let mut result = String::new();
file.open_file()?.read_to_string(&mut result)?;
assert_eq!(&result, "Hello, world!");
sourcepub fn append_file(&self) -> VfsResult<Box<dyn Write>>
pub fn append_file(&self) -> VfsResult<Box<dyn Write>>
Opens the file at this path for appending
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file()?, "Hello, ")?;
write!(file.append_file()?, "world!")?;
let mut result = String::new();
file.open_file()?.read_to_string(&mut result)?;
assert_eq!(&result, "Hello, world!");
sourcepub fn remove_file(&self) -> VfsResult<()>
pub fn remove_file(&self) -> VfsResult<()>
Removes the file at this path
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file()?, "Hello, ")?;
assert!(file.exists()?);
file.remove_file()?;
assert!(!file.exists()?);
sourcepub fn remove_dir(&self) -> VfsResult<()>
pub fn remove_dir(&self) -> VfsResult<()>
Removes the directory at this path
The directory must be empty.
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir();
assert!(directory.exists()?);
directory.remove_dir()?;
assert!(!directory.exists()?);
sourcepub fn remove_dir_all(&self) -> VfsResult<()>
pub fn remove_dir_all(&self) -> VfsResult<()>
Ensures that the directory at this path is removed, recursively deleting all contents if necessary
Returns successfully if directory does not exist
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
directory.join("bar")?.create_dir_all();
assert!(directory.exists()?);
directory.remove_dir_all()?;
assert!(!directory.exists()?);
sourcepub fn metadata(&self) -> VfsResult<VfsMetadata>
pub fn metadata(&self) -> VfsResult<VfsMetadata>
Returns the file metadata for the file at this path
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir();
assert_eq!(directory.metadata()?.len, 0);
assert_eq!(directory.metadata()?.file_type, VfsFileType::Directory);
let file = path.join("bar.txt")?;
write!(file.create_file()?, "Hello, world!")?;
assert_eq!(file.metadata()?.len, 13);
assert_eq!(file.metadata()?.file_type, VfsFileType::File);
sourcepub fn is_file(&self) -> VfsResult<bool>
pub fn is_file(&self) -> VfsResult<bool>
Returns true
if the path exists and is pointing at a regular file, otherwise returns false
.
Note that this call may fail if the file’s existence cannot be determined or the metadata can not be retrieved
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir()?;
let file = path.join("foo.txt")?;
file.create_file()?;
assert!(!directory.is_file()?);
assert!(file.is_file()?);
sourcepub fn is_dir(&self) -> VfsResult<bool>
pub fn is_dir(&self) -> VfsResult<bool>
Returns true
if the path exists and is pointing at a directory, otherwise returns false
.
Note that this call may fail if the directory’s existence cannot be determined or the metadata can not be retrieved
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir()?;
let file = path.join("foo.txt")?;
file.create_file()?;
assert!(directory.is_dir()?);
assert!(!file.is_dir()?);
sourcepub fn exists(&self) -> VfsResult<bool>
pub fn exists(&self) -> VfsResult<bool>
Returns true if a file or directory exists at this path, false otherwise
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo")?;
assert!(!directory.exists()?);
directory.create_dir();
assert!(directory.exists()?);
sourcepub fn filename(&self) -> String
pub fn filename(&self) -> String
Returns the filename portion of this path
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo/bar.txt")?;
assert_eq!(&file.filename(), "bar.txt");
sourcepub fn extension(&self) -> Option<String>
pub fn extension(&self) -> Option<String>
Returns the extension portion of this path
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
assert_eq!(path.join("foo/bar.txt")?.extension(), Some("txt".to_string()));
assert_eq!(path.join("foo/bar.txt.zip")?.extension(), Some("zip".to_string()));
assert_eq!(path.join("foo/bar")?.extension(), None);
sourcepub fn parent(&self) -> Option<Self>
pub fn parent(&self) -> Option<Self>
Returns the parent path of this portion of this path
Returns None
if this is a root path
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
assert_eq!(path.parent(), None);
assert_eq!(path.join("foo/bar")?.parent(), Some(path.join("foo")?));
assert_eq!(path.join("foo")?.parent(), Some(path));
sourcepub fn walk_dir(&self) -> VfsResult<WalkDirIterator>
pub fn walk_dir(&self) -> VfsResult<WalkDirIterator>
Recursively iterates over all the directories and files at this path
Directories are visited before their children
Note that the iterator items can contain errors, usually when directories are removed during the iteration. The returned paths may also point to non-existant files if there is concurrent removal.
Also note that loops in the file system hierarchy may cause this iterator to never terminate.
let root = VfsPath::new(MemoryFS::new());
root.join("foo/bar")?.create_dir_all()?;
root.join("fizz/buzz")?.create_dir_all()?;
root.join("foo/bar/baz")?.create_file()?;
let mut directories = root.walk_dir()?.collect::<VfsResult<Vec<_>>>()?;
directories.sort_by_key(|path| path.as_str().to_string());
let expected = vec!["fizz", "fizz/buzz", "foo", "foo/bar", "foo/bar/baz"].iter().map(|path| root.join(path)).collect::<VfsResult<Vec<_>>>()?;
assert_eq!(directories, expected);
sourcepub fn read_to_string(&self) -> VfsResult<String>
pub fn read_to_string(&self) -> VfsResult<String>
Reads a complete file to a string
Returns an error if the file does not exist or is not valid UTF-8
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file()?, "Hello, world!")?;
let result = file.read_to_string()?;
assert_eq!(&result, "Hello, world!");
sourcepub fn copy_file(&self, destination: &VfsPath) -> VfsResult<()>
pub fn copy_file(&self, destination: &VfsPath) -> VfsResult<()>
Copies a file to a new destination
The destination must not exist, but its parent directory must
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let src = path.join("foo.txt")?;
write!(src.create_file()?, "Hello, world!")?;
let dest = path.join("bar.txt")?;
src.copy_file(&dest)?;
assert_eq!(dest.read_to_string()?, "Hello, world!");
sourcepub fn move_file(&self, destination: &VfsPath) -> VfsResult<()>
pub fn move_file(&self, destination: &VfsPath) -> VfsResult<()>
Moves or renames a file to a new destination
The destination must not exist, but its parent directory must
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let src = path.join("foo.txt")?;
write!(src.create_file()?, "Hello, world!")?;
let dest = path.join("bar.txt")?;
src.move_file(&dest)?;
assert_eq!(dest.read_to_string()?, "Hello, world!");
assert!(!src.exists()?);
sourcepub fn copy_dir(&self, destination: &VfsPath) -> VfsResult<u64>
pub fn copy_dir(&self, destination: &VfsPath) -> VfsResult<u64>
Copies a directory to a new destination, recursively
The destination must not exist, but the parent directory must
Returns the number of files copied
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let src = path.join("foo")?;
src.join("dir")?.create_dir_all()?;
let dest = path.join("bar.txt")?;
src.copy_dir(&dest)?;
assert!(dest.join("dir")?.exists()?);
sourcepub fn move_dir(&self, destination: &VfsPath) -> VfsResult<()>
pub fn move_dir(&self, destination: &VfsPath) -> VfsResult<()>
Moves a directory to a new destination, including subdirectories and files
The destination must not exist, but its parent directory must
use vfs::{MemoryFS, VfsError, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let src = path.join("foo")?;
src.join("dir")?.create_dir_all()?;
let dest = path.join("bar.txt")?;
src.move_dir(&dest)?;
assert!(dest.join("dir")?.exists()?);
assert!(!src.join("dir")?.exists()?);
Trait Implementations
sourceimpl<T: FileSystem> From<T> for VfsPath
impl<T: FileSystem> From<T> for VfsPath
impl Eq for VfsPath
Auto Trait Implementations
impl !RefUnwindSafe for VfsPath
impl Send for VfsPath
impl Sync for VfsPath
impl Unpin for VfsPath
impl !UnwindSafe for VfsPath
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more