Crate rivia_vfs

Crate rivia_vfs 

Source
Expand description

rivia-vfs provides an ergonomic veneer over rivia’s VirtualFileSystem implementation

This ergonomic approach allows for a clean namespaced usage of the VirtualFileSystem implementation e.g. vfs::abs(). Additionally a mechanism is provided to seamlessly switch out the VFS provider dynamically.

§Switching VFS providers

By default the VFS backend provider will be set to Stdfs which is an implementation wrapping the standard library std::fs and related functions to satisfy the VirtualFileSystem trait; however you change the backend provider by simply calling the vfs::set() function and pass in a different variant of the Vfs enum.

§Example

use rivia_vfs::prelude::*;

fn main() {
    // Simply remove this line to default to the real filesystem.
    vfs::set_memfs().unwrap();

    let config = load_config();
    assert_eq!(config, "this is a test");
    println!("VFS test passed");
}

// Load an example application configuration file using VFS.
// This allows you to test with a memory backed VFS implementation during testing and with
// the real filesystem during production.
fn load_config() -> String {
    let dir = PathBuf::from("/etc/xdg");
    vfs::mkdir_p(&dir).unwrap();
    let filepath = dir.mash("rivia.toml");
    vfs::write_all(&filepath, "this is a test").unwrap();
    assert_eq!(vfs::config_dir("rivia.toml").unwrap().to_str().unwrap(), "/etc/xdg");

    if let Some(config_dir) = vfs::config_dir("rivia.toml") {
        let path = config_dir.mash("rivia.toml");
        return vfs::read_all(&path).unwrap();
    }
    "".into()
}

Modules§

assert
prelude
All essential symbols in a simple consumable form

Macros§

assert_copyfile
Assert the copy of a file
assert_exists
Assert that a file or directory exists
assert_is_dir
Assert that the given path exists and is a directory
assert_is_file
Assert that the given path exists and is a file
assert_is_symlink
Assert that the given path exists and is a symlink
assert_memfs_setup
Setup Vfs testing components with Memfs provider
assert_mkdir_m
Assert the creation of the given directory with the given mode
assert_mkdir_p
Assert the creation of the given directory.
assert_mkfile
Assert the creation of a file. If the file exists no change is made
assert_no_dir
Assert that the given path isn’t a directory
assert_no_exists
Assert the given path doesn’t exist
assert_no_file
Assert that the given path isn’t a file
assert_no_symlink
Assert that the given path isn’t a symlink
assert_read_all
Assert data read from the file matches the input data
assert_readlink
Assert the reading of a link’s target relative path
assert_readlink_abs
Assert the reading of a link’s target absolute path
assert_remove
Assert the removal of the target file or directory
assert_remove_all
Assert the removal of the target path
assert_setup
Setup Vfs testing components using the current provider is
assert_stdfs_setup
Setup Vfs testing components with Stdfs provider
assert_symlink
Assert the creation of a symlink. If the symlink exists no change is made
assert_write_all
Assert data is written to the given file

Structs§

VFS
VFS is a virtual filesystem singleton providing an implementation of Vfs that defaults to Stdfs but can be changed dynamically to any variant of the Vfs enum.

Functions§

abs
Return the path in an absolute clean form
all_dirs
Returns all dirs for the given path recursively
all_files
Returns all files for the given path recursively
all_paths
Returns all paths for the given path recursively
append
Opens a file in append mode
append_all
Append the given data to to the target file
append_line
Append the given line to to the target file including a newline
append_lines
Append the given lines to to the target file including newlines
chmod
Change all file/dir permissions recursivly to mode
chmod_b
Returns a new Chmod builder for advanced chmod options
chown
Change the ownership of the path recursivly
chown_b
Creates new Chown for use with the builder pattern
config_dir
Returns the highest priority active configuration directory.
copy
Copies src to dst recursively
copy_b
Creates a new Copier for use with the builder pattern
cwd
Returns the current working directory
dirs
Returns all directories for the given path, sorted by name
entries
Returns an iterator over the given path
entry
Return a virtual filesystem entry for the given path
exists
Returns true if the path exists
files
Returns all files for the given path, sorted by name
gid
Returns the group ID of the owner of this file
is_dir
Returns true if the given path exists and is a directory
is_exec
Returns true if the given path exists and is readonly
is_file
Returns true if the given path exists and is a file
is_readonly
Returns true if the given path exists and is readonly
is_symlink
Returns true if the given path exists and is a symlink
is_symlink_dir
Returns true if the given path exists and is a symlink pointing to a directory
is_symlink_file
Returns true if the given path exists and is a symlink pointing to a file
mkdir_m
Creates the given directory and any parent directories needed with the given mode
mkdir_p
Creates the given directory and any parent directories needed
mkfile
Create an empty file similar to the linux touch command
mkfile_m
Wraps mkfile allowing for setting the file’s mode.
mode
Returns the permissions for a file, directory or link
move_p
Move a file or directory
owner
Returns the (user ID, group ID) of the owner of this file
paths
Returns all paths for the given path, sorted by name
read
Attempts to open a file in readonly mode
read_all
Read all data from the given file and return it as a String
read_lines
Read the given file and returns it as lines in a vector
readlink
Returns the relative path of the target the link points to
readlink_abs
Returns the absolute path of the target the link points to
remove
Removes the given empty directory or file
remove_all
Removes the given directory after removing all of its contents
root
Returns the current root directory
set
Set the current vfs backend being used
set_cwd
Set the current working directory
set_memfs
Switch the current vfs provider to Memfs if not already
set_stdfs
Switch the current vfs provider to Stdfs if not already
symlink
Creates a new symbolic link
uid
Returns the user ID of the owner of this file
write
Opens a file in write-only mode
write_all
Write the given data to to the target file
write_lines
Write the given lines to to the target file including final newline