Struct vfs::async_vfs::path::AsyncVfsPath
source · pub struct AsyncVfsPath { /* private fields */ }
Expand description
A virtual filesystem path, identifying a single file or directory in this virtual filesystem
Implementations§
source§impl AsyncVfsPath
impl AsyncVfsPath
sourcepub fn new<T: AsyncFileSystem>(filesystem: T) -> Self
pub fn new<T: AsyncFileSystem>(filesystem: T) -> Self
Creates a root path for the given filesystem
let path = AsyncVfsPath::new(AsyncPhysicalFS::new("."));
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the string representation of this path
let path = AsyncVfsPath::new(AsyncPhysicalFS::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 = AsyncVfsPath::new(AsyncPhysicalFS::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) -> AsyncVfsPath
pub fn root(&self) -> AsyncVfsPath
Returns the root path of this filesystem
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo/bar")?;
assert_eq!(directory.root(), path);
sourcepub fn is_root(&self) -> bool
pub fn is_root(&self) -> bool
Returns true if this is the root path
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
assert!(path.is_root());
let path = path.join("foo/bar")?;
assert!(! path.is_root());
sourcepub async fn create_dir(&self) -> VfsResult<()>
pub async 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 = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir().await?;
assert!(directory.exists().await?);
assert_eq!(directory.metadata().await?.file_type, VfsFileType::Directory);
sourcepub async fn create_dir_all(&self) -> VfsResult<()>
pub async fn create_dir_all(&self) -> VfsResult<()>
Creates the directory at this path, also creating parent directories as necessary
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo/bar")?;
directory.create_dir_all().await?;
assert!(directory.exists().await?);
assert_eq!(directory.metadata().await?.file_type, VfsFileType::Directory);
let parent = path.join("foo")?;
assert!(parent.exists().await?);
assert_eq!(parent.metadata().await?.file_type, VfsFileType::Directory);
sourcepub async fn read_dir(
&self
) -> VfsResult<Box<dyn Unpin + Stream<Item = AsyncVfsPath> + Send>>
pub async fn read_dir( &self ) -> VfsResult<Box<dyn Unpin + Stream<Item = AsyncVfsPath> + Send>>
Iterates over all entries of this directory path
use futures::stream::Collect;
use futures::stream::StreamExt;
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
path.join("foo")?.create_dir().await?;
path.join("bar")?.create_dir().await?;
let mut directories: Vec<_> = path.read_dir().await?.collect().await;
directories.sort_by_key(|path| path.as_str().to_string());
assert_eq!(directories, vec![path.join("bar")?, path.join("foo")?]);
sourcepub async fn create_file(&self) -> VfsResult<Box<dyn Write + Send + Unpin>>
pub async fn create_file(&self) -> VfsResult<Box<dyn Write + Send + Unpin>>
Creates a file at this path for writing, overwriting any existing file
use async_std::io:: {ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file().await?, "Hello, world!").await?;
let mut result = String::new();
file.open_file().await?.read_to_string(&mut result).await?;
assert_eq!(&result, "Hello, world!");
sourcepub async fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead + Send + Unpin>>
pub async fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead + Send + Unpin>>
Opens the file at this path for reading
use async_std::io:: {ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file().await?, "Hello, world!").await?;
let mut result = String::new();
file.open_file().await?.read_to_string(&mut result).await?;
assert_eq!(&result, "Hello, world!");
sourcepub async fn append_file(&self) -> VfsResult<Box<dyn Write + Send + Unpin>>
pub async fn append_file(&self) -> VfsResult<Box<dyn Write + Send + Unpin>>
Opens the file at this path for appending
use async_std::io:: {ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file().await?, "Hello, ").await?;
write!(file.append_file().await?, "world!").await?;
let mut result = String::new();
file.open_file().await?.read_to_string(&mut result).await?;
assert_eq!(&result, "Hello, world!");
sourcepub async fn remove_file(&self) -> VfsResult<()>
pub async fn remove_file(&self) -> VfsResult<()>
Removes the file at this path
use async_std::io:: {ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file().await?, "Hello, ").await?;
assert!(file.exists().await?);
file.remove_file().await?;
assert!(!file.exists().await?);
sourcepub async fn remove_dir(&self) -> VfsResult<()>
pub async fn remove_dir(&self) -> VfsResult<()>
Removes the directory at this path
The directory must be empty.
use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
use vfs::VfsError;
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir().await;
assert!(directory.exists().await?);
directory.remove_dir().await?;
assert!(!directory.exists().await?);
sourcepub fn remove_dir_all<'life_self, 'async_recursion>(
&'life_self self
) -> Pin<Box<dyn Future<Output = VfsResult<()>> + Send + 'async_recursion>>where
'life_self: 'async_recursion,
pub fn remove_dir_all<'life_self, 'async_recursion>(
&'life_self self
) -> Pin<Box<dyn Future<Output = VfsResult<()>> + Send + 'async_recursion>>where
'life_self: 'async_recursion,
Ensures that the directory at this path is removed, recursively deleting all contents if necessary
Returns successfully if directory does not exist
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
directory.join("bar")?.create_dir_all().await?;
assert!(directory.exists().await?);
directory.remove_dir_all().await?;
assert!(!directory.exists().await?);
sourcepub async fn metadata(&self) -> VfsResult<VfsMetadata>
pub async fn metadata(&self) -> VfsResult<VfsMetadata>
Returns the file metadata for the file at this path
use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
use vfs::{VfsError, VfsFileType, VfsMetadata};
use async_std::io::WriteExt;
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir().await?;
assert_eq!(directory.metadata().await?.len, 0);
assert_eq!(directory.metadata().await?.file_type, VfsFileType::Directory);
let file = path.join("bar.txt")?;
write!(file.create_file().await?, "Hello, world!").await?;
assert_eq!(file.metadata().await?.len, 13);
assert_eq!(file.metadata().await?.file_type, VfsFileType::File);
sourcepub async fn set_creation_time(&self, time: SystemTime) -> VfsResult<()>
pub async fn set_creation_time(&self, time: SystemTime) -> VfsResult<()>
Sets the files creation timestamp at this path
use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
use vfs::{VfsError, VfsFileType, VfsMetadata, VfsPath};
use async_std::io::WriteExt;
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
file.create_file();
let time = std::time::SystemTime::now();
file.set_creation_time(time).await?;
assert_eq!(file.metadata().await?.len, 0);
assert_eq!(file.metadata().await?.created, Some(time));
sourcepub async fn set_modification_time(&self, time: SystemTime) -> VfsResult<()>
pub async fn set_modification_time(&self, time: SystemTime) -> VfsResult<()>
Sets the files modification timestamp at this path
use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
use vfs::{VfsError, VfsFileType, VfsMetadata, VfsPath};
use async_std::io::WriteExt;
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
file.create_file();
let time = std::time::SystemTime::now();
file.set_modification_time(time).await?;
assert_eq!(file.metadata().await?.len, 0);
assert_eq!(file.metadata().await?.modified, Some(time));
sourcepub async fn set_access_time(&self, time: SystemTime) -> VfsResult<()>
pub async fn set_access_time(&self, time: SystemTime) -> VfsResult<()>
Sets the files access timestamp at this path
use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
use vfs::{VfsError, VfsFileType, VfsMetadata, VfsPath};
use async_std::io::WriteExt;
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
file.create_file();
let time = std::time::SystemTime::now();
file.set_access_time(time).await?;
assert_eq!(file.metadata().await?.len, 0);
assert_eq!(file.metadata().await?.accessed, Some(time));
sourcepub async fn is_file(&self) -> VfsResult<bool>
pub async 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
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir().await?;
let file = path.join("foo.txt")?;
file.create_file().await?;
assert!(!directory.is_file().await?);
assert!(file.is_file().await?);
sourcepub async fn is_dir(&self) -> VfsResult<bool>
pub async 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
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
directory.create_dir().await?;
let file = path.join("foo.txt")?;
file.create_file().await?;
assert!(directory.is_dir().await?);
assert!(!file.is_dir().await?);
sourcepub async fn exists(&self) -> VfsResult<bool>
pub async fn exists(&self) -> VfsResult<bool>
Returns true if a file or directory exists at this path, false otherwise
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let directory = path.join("foo")?;
assert!(!directory.exists().await?);
directory.create_dir().await?;
assert!(directory.exists().await?);
sourcepub fn filename(&self) -> String
pub fn filename(&self) -> String
Returns the filename portion of this path
let path = AsyncVfsPath::new(AsyncMemoryFS::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
let path = AsyncVfsPath::new(AsyncMemoryFS::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) -> Self
pub fn parent(&self) -> Self
Returns the parent path of this portion of this path
Root will return itself.
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
assert_eq!(path.parent(), path.root());
assert_eq!(path.join("foo/bar")?.parent(), path.join("foo")?);
assert_eq!(path.join("foo")?.parent(), path);
sourcepub async fn walk_dir(&self) -> VfsResult<WalkDirIterator>
pub async 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.
use futures::stream::StreamExt;
let root = AsyncVfsPath::new(AsyncMemoryFS::new());
root.join("foo/bar")?.create_dir_all().await?;
root.join("fizz/buzz")?.create_dir_all().await?;
root.join("foo/bar/baz")?.create_file().await?;
let mut directories = root.walk_dir().await?.map(|res| res.unwrap()).collect::<Vec<_>>().await;
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 async fn read_to_string(&self) -> VfsResult<String>
pub async 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 async_std::io::{ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let file = path.join("foo.txt")?;
write!(file.create_file().await?, "Hello, world!").await?;
let result = file.read_to_string().await?;
assert_eq!(&result, "Hello, world!");
sourcepub async fn copy_file(&self, destination: &AsyncVfsPath) -> VfsResult<()>
pub async fn copy_file(&self, destination: &AsyncVfsPath) -> VfsResult<()>
Copies a file to a new destination
The destination must not exist, but its parent directory must
use async_std::io::{ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let src = path.join("foo.txt")?;
write!(src.create_file().await?, "Hello, world!").await?;
let dest = path.join("bar.txt")?;
src.copy_file(&dest).await?;
assert_eq!(dest.read_to_string().await?, "Hello, world!");
sourcepub async fn move_file(&self, destination: &AsyncVfsPath) -> VfsResult<()>
pub async fn move_file(&self, destination: &AsyncVfsPath) -> VfsResult<()>
Moves or renames a file to a new destination
The destination must not exist, but its parent directory must
use async_std::io::{ReadExt, WriteExt};
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let src = path.join("foo.txt")?;
write!(src.create_file().await?, "Hello, world!").await?;
let dest = path.join("bar.txt")?;
src.move_file(&dest).await?;
assert_eq!(dest.read_to_string().await?, "Hello, world!");
assert!(!src.exists().await?);
sourcepub async fn copy_dir(&self, destination: &AsyncVfsPath) -> VfsResult<u64>
pub async fn copy_dir(&self, destination: &AsyncVfsPath) -> 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
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let src = path.join("foo")?;
src.join("dir")?.create_dir_all().await?;
let dest = path.join("bar.txt")?;
src.copy_dir(&dest).await?;
assert!(dest.join("dir")?.exists().await?);
sourcepub async fn move_dir(&self, destination: &AsyncVfsPath) -> VfsResult<()>
pub async fn move_dir(&self, destination: &AsyncVfsPath) -> VfsResult<()>
Moves a directory to a new destination, including subdirectories and files
The destination must not exist, but its parent directory must
let path = AsyncVfsPath::new(AsyncMemoryFS::new());
let src = path.join("foo")?;
src.join("dir")?.create_dir_all().await?;
let dest = path.join("bar.txt")?;
src.move_dir(&dest).await?;
assert!(dest.join("dir")?.exists().await?);
assert!(!src.join("dir")?.exists().await?);
Trait Implementations§
source§impl Clone for AsyncVfsPath
impl Clone for AsyncVfsPath
source§fn clone(&self) -> AsyncVfsPath
fn clone(&self) -> AsyncVfsPath
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more