pub struct VfsPath { /* private fields */ }
Expand description

A virtual filesystem path, identifying a single file or directory in this virtual filesystem

Implementations

Creates a root path for the given filesystem

let path = VfsPath::new(PhysicalFS::new("."));

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");

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("..")?);

Returns the root path of this filesystem

let path = VfsPath::new(MemoryFS::new());
let directory = path.join("foo/bar")?;

assert_eq!(directory.root(), path);

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);

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);

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")?]);

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!");

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!");

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!");

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()?);

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()?);

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()?);

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);

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()?);

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()?);

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()?);

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");

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);

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));

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);

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!");

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!");

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()?);

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()?);

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.