LocalSaveFile
About - Usage - Related - License
About
Save and load structs from a local file. A convenience wrapper around the savefile crate.
LocalSaveFile takes care of where and how a struct should be saved to disk. savefile allows for serialization and compression of a rust data-structure while directories-rs decides where that file should go.
This crate is not meant to be used as a database or anything more complex then a simple struct. Carrying over from savefile, this crate could be used, for example, for a save file in a game.
Why
I have been making a few toy program's in rust and kept finding a need to have some form of persistent storage. I did not want anything that was complicated to implement, and so, something as simple as attaching an attribute to a struct seemed like a good idea.
Usage
[!NOTE] Currently, only
structshave been tested and are the scope of this crate.
Requirements
Cargo
[!NOTE] savefile also needs to be added with cargo to be used by the external macro.
Minimal Example
[!IMPORTANT] The macros Default and Savefile are automatically set to be derived. In any case, use
localsavefile_implinstead oflocalsavefileto manually derive them.
use ;
let foo = MySave ;
foo.save;
let bar = load_default;
let mut baz = MySave ;
baz.load;
assert_eq!; // Should never trigger
assert_eq!; // Should never trigger
remove_file;
Persistent File
If you wish to maintain the underlying file open, as in, not having to reopen it each time save or load is called, a file handler can be added to your struct through the parameter persist = true. This will modify your struct and add an additional field. It's usage is the same as the non-persistent version, with a few caveats as shown.
use ;
let mut foo = MySavePersist ;
// foo.open_default(); // You should call open or open_default first
// but foo.save() will also open_default if needed
foo.save; // Save now requires foo to be mutable
let mut bar = load_default;
assert_eq!; // Should never trigger
foo.close;
bar.close; // Requires bar to be mutable
// Close any instances before removing the file
remove_file;
[!CAUTION] Because
localsavefile(persist = true)modifies your struct, it is important to place it before any derives that must be aware of every field, such as when usinglocalsavefile_impl.// First localsavefile // Then whatever else ...In this case, this ensures the added field gets processed by
DefaultandSavefile.
Options
By default, the underlying file name is based off a sanitized combination of module_path!, called from where the struct is defined, and the struct name.
The directory where files are stored is based off of directories::ProjectDirs.data_dir, where the name and first author in your Cargo.toml are used as parameters. Author does not need to be defined, but should be anyways.
As you can imagine, changing anything that these defaults use will sneakily change what your struct loads. The following options shown allow to override any of the mention values to maintain a static path.
The following is what the Minimal Example will output as on my windows machine using localsavefile-test.
The version option takes a u32 and is passed to the underlying savefile crate. Take a look at the version section of that crate for more information, as that is all still relevant on this struct.
Related
- avl/savefile
- dirs-dev/directories-rs
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.