Disk
Disk: serde
+ directories
+ a whole bunch of file formats as Traits
.
This crate is for (de)serializing to/from various file formats (provided by serde
) to/from disk locations that follow OS-specific specifications/conventions (provided by directories
).
All errors returned are of type anyhow::Error
.
File Formats
Use the feature flag full
to enable everything.
File Format | Feature flag to enable |
---|---|
Bincode | bincode |
JSON | json |
TOML | toml |
YAML | yaml |
Pickle | pickle |
MessagePack | messagepack |
BSON | bson |
Plain Text | plain |
Empty File | empty |
Example
Defining our struct, State
:
use *; // Necessary imports to get things working.
use ; // <- TOML trait & macro.
use ;
// To make this struct a file, use the following macro:
//
// |- 1. The file format used will be TOML.
// |
// | |- 2. The struct "State" will be used.
// | |
// | | |- 3. It will be saved in the OS Data directory.
// | | |
// | | | |- 4. The main project directory is called "MyProject".
// | | | |
// | | | | |- 6. It won't be in any sub-directories.
// | | | | |
// | | | | | |- 7. The file name will be "state.toml".
// v v v v v v
toml_file!;
// <- Your data must implement `serde`.
Saving State
to disk:
let state = State ;
// This saves to `~/.local/share/myproject/state.toml`
state.save.unwrap;
Creating a State
from disk:
// This reads from `~/.local/share/myproject/state.toml`
let state = from_file.unwrap;