Struct vfs::path::VfsPath

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

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

Implementations§

source§

impl VfsPath

source

pub fn new<T: FileSystem>(filesystem: T) -> Self

Creates a root path for the given filesystem

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

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

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

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

pub fn is_root(&self) -> bool

Returns true if this is the root path

let path = VfsPath::new(MemoryFS::new());
assert!(path.is_root());
let path = path.join("foo/bar")?;
assert!(! path.is_root());
source

pub fn create_dir(&self) -> VfsResult<()>

Creates the directory at this path

Note that the parent directory must exist, while the given path must not exist.

Returns VfsErrorKind::FileExists if a file already exists at the given path Returns VfsErrorKind::DirectoryExists if a directory already exists at the given path

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

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

pub fn read_dir(&self) -> VfsResult<Box<dyn Iterator<Item = VfsPath> + Send>>

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

pub fn create_file(&self) -> VfsResult<Box<dyn SeekAndWrite + Send>>

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

pub fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead + Send>>

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

pub fn append_file(&self) -> VfsResult<Box<dyn SeekAndWrite + Send>>

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

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

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

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

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

pub fn set_creation_time(&self, time: SystemTime) -> VfsResult<()>

Sets the files creation timestamp at this path

use std::time::SystemTime;
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
file.create_file();

let time = SystemTime::now();
file.set_creation_time(time);

assert_eq!(file.metadata()?.len, 0);
assert_eq!(file.metadata()?.created, Some(time));
source

pub fn set_modification_time(&self, time: SystemTime) -> VfsResult<()>

Sets the files modification timestamp at this path

use std::time::SystemTime;
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
file.create_file();

let time = SystemTime::now();
file.set_modification_time(time);

assert_eq!(file.metadata()?.len, 0);
assert_eq!(file.metadata()?.modified, Some(time));
source

pub fn set_access_time(&self, time: SystemTime) -> VfsResult<()>

Sets the files access timestamp at this path

use std::time::SystemTime;
use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());
let file = path.join("foo.txt")?;
file.create_file();

let time = SystemTime::now();
file.set_access_time(time);

assert_eq!(file.metadata()?.len, 0);
assert_eq!(file.metadata()?.accessed, Some(time));
source

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

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

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

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

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

pub fn parent(&self) -> Self

Returns the parent path of this portion of this path

Root will return itself.

use vfs::{MemoryFS, VfsError, VfsFileType, VfsMetadata, VfsPath};
let path = VfsPath::new(MemoryFS::new());

assert_eq!(path.parent(), path.root());
assert_eq!(path.join("foo/bar")?.parent(), path.join("foo")?);
assert_eq!(path.join("foo")?.parent(), path);
source

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

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

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

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

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

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§

source§

impl Clone for VfsPath

source§

fn clone(&self) -> VfsPath

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for VfsPath

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: FileSystem> From<T> for VfsPath

source§

fn from(filesystem: T) -> Self

Converts to this type from the input type.
source§

impl PartialEq for VfsPath

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for VfsPath

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more