pub unsafe trait Empty {
const OS_DIRECTORY: Dir;
const PROJECT_DIRECTORY: &'static str;
const SUB_DIRECTORIES: &'static str;
const FILE: &'static str;
const FILE_EXT: &'static str;
const FILE_NAME: &'static str;
const FILE_NAME_GZIP: &'static str;
const FILE_NAME_TMP: &'static str;
const FILE_NAME_GZIP_TMP: &'static str;
Show 15 methods
// Provided methods
fn mkdir() -> Result<(), Error> { ... }
fn rm_rf() -> Result<(), Error> { ... }
fn rm() -> Result<(), Error> { ... }
fn exists() -> Result<bool, Error> { ... }
fn project_dir() -> &'static str { ... }
fn sub_dirs() -> &'static str { ... }
fn file_name() -> &'static str { ... }
fn file_ext() -> &'static str { ... }
fn file() -> &'static str { ... }
fn file_gzip() -> &'static str { ... }
fn file_tmp() -> &'static str { ... }
fn file_gzip_tmp() -> &'static str { ... }
fn base_path() -> Result<PathBuf, Error> { ... }
fn absolute_path() -> Result<PathBuf, Error> { ... }
fn touch() -> Result<(), Error> { ... }
}Expand description
Empty file
This is a an empty file that
- Contains no data
- Doesn’t need
serde - Inherits useful
PATHmethods.
Typically used for file-based signals.
If you implement this on a struct that contains data, the data will be ignored
and an empty file will always be created.
The file created will have no file extension, e.g:
disk::empty!(Hello, Dir::Data, "disk_test", "signal", "hello");
#[derive(Serialize, Deserialize)]
struct Hello {
data: bool,
}
// The filename should be "hello".
assert!(Hello::file_name() == "hello");
// Create the file.
Hello::touch().unwrap();
// Make sure it (and the directories) exist.
assert!(Hello::exists().unwrap());
// Delete the project directory.
Hello::rm_rf().unwrap();
// Make sure the file no longer exist.
assert!(!Hello::exists().unwrap());This creates a file called hello, containing no data. The bool is ignored.
The PATH on Linux would be: ~/.local/share/disk_test/signal/hello.
Safety
When manually implementing, you are promising that the PATH’s manually specified are correct.
Required Associated Constants§
sourceconst OS_DIRECTORY: Dir
const OS_DIRECTORY: Dir
Which OS directory it will be saved in.
sourceconst PROJECT_DIRECTORY: &'static str
const PROJECT_DIRECTORY: &'static str
What the main project directory will be.
sourceconst SUB_DIRECTORIES: &'static str
const SUB_DIRECTORIES: &'static str
Optional sub directories in between the project directory and file.
sourceconst FILE_NAME_GZIP: &'static str
const FILE_NAME_GZIP: &'static str
What the gzip variant of the filename will be.
sourceconst FILE_NAME_TMP: &'static str
const FILE_NAME_TMP: &'static str
What the tmp variant of the filename will be.
sourceconst FILE_NAME_GZIP_TMP: &'static str
const FILE_NAME_GZIP_TMP: &'static str
What the gzip + tmp variant of the filename will be.
Provided Methods§
sourcefn mkdir() -> Result<(), Error>
fn mkdir() -> Result<(), Error>
Create the directories leading up-to the file.
This is not necessary when using any variant of
Self::save() as the directories are created implicitly.
sourcefn rm_rf() -> Result<(), Error>
fn rm_rf() -> Result<(), Error>
Recursively remove this file’s project directory.
This deletes all directories starting from Self::PROJECT_DIRECTORY.
For example:
disk::toml!(State, disk::Dir::Data, "MyProject", "sub_dir", "state");This project’s file would be located at ~/.local/share/myproject.
This is the PATH that gets removed recursively.
This is akin to running:
rm -rf ~/.local/share/myprojectThe input to all disk macros are sanity checked.
The worst you can do with this function is delete your project’s directory.
This function calls std::fs::remove_dir_all, which does not follow symlinks.
sourcefn rm() -> Result<(), Error>
fn rm() -> Result<(), Error>
Try deleting the file.
This will return success if the file doesn’t exist or if deleted.
It will return failure if the file existed but could not be deleted or if any other error occurs.
sourcefn exists() -> Result<bool, Error>
fn exists() -> Result<bool, Error>
Check if the file exists.
true== The file exists.false== The file does not exist.anyhow::Error== There was an error, existance is unknown.
sourcefn project_dir() -> &'static str
fn project_dir() -> &'static str
The main project directory.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::project_dir() == Data::PROJECT_DIRECTORY);sourcefn sub_dirs() -> &'static str
fn sub_dirs() -> &'static str
The directories after the main project directory, before the file. (the first directory specified in the SUB_DIRECTORIES constant).
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "sub_directory", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::sub_dirs() == Data::SUB_DIRECTORIES);sourcefn file_name() -> &'static str
fn file_name() -> &'static str
The filename + extension associated with this struct.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::file_name() == Data::FILE_NAME);sourcefn file_ext() -> &'static str
fn file_ext() -> &'static str
The extension associated with this struct.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::file_ext() == Data::FILE_EXT);sourcefn file() -> &'static str
fn file() -> &'static str
The file associated with this struct WITHOUT the extension.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::file() == Data::FILE);sourcefn file_gzip() -> &'static str
fn file_gzip() -> &'static str
The gzip variant of the filename + extension associated with this struct.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::file_gzip() == Data::FILE_NAME_GZIP);sourcefn file_tmp() -> &'static str
fn file_tmp() -> &'static str
The tmp variant of the filename + extension associated with this struct.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::file_tmp() == Data::FILE_NAME_TMP);sourcefn file_gzip_tmp() -> &'static str
fn file_gzip_tmp() -> &'static str
The gzip + tmp variant of the filename + extension associated with this struct.
You can also access this directly on your type:
disk::toml!(Data, disk::Dir::Cache, "MyProject", "", "data");
#[derive(Serialize, Deserialize)]
struct Data(u64);
assert!(Data::file_gzip_tmp() == Data::FILE_NAME_GZIP_TMP);sourcefn base_path() -> Result<PathBuf, Error>
fn base_path() -> Result<PathBuf, Error>
The base path associated with this struct (PATH leading up to the file).
sourcefn absolute_path() -> Result<PathBuf, Error>
fn absolute_path() -> Result<PathBuf, Error>
The absolute PATH of the file associated with this struct.