save-load-traits
The save-load-traits
crate provides traits for saving and loading objects asynchronously to and from files and directories. It also includes a blanket implementation for loading objects from a directory using the LoadFromFile
trait.
Key Features
- SaveToFile Trait: Defines a trait for saving objects to a file asynchronously.
- LoadFromFile Trait: Defines a trait for loading objects from a file asynchronously.
- LoadFromDirectory Trait: Extends the functionality of loading from a directory, supporting automatic loading of files from a directory based on the
LoadFromFile
trait.
Traits and Implementations
SaveToFile Trait
The SaveToFile
trait allows objects to be saved asynchronously to a file.
LoadFromFile Trait
The LoadFromFile
trait allows objects to be loaded asynchronously from a file.
LoadFromDirectory Trait
The LoadFromDirectory
trait is designed for asynchronously loading objects from a directory. It provides a blanket implementation for any type that implements LoadFromFile
.
Error Handling
This crate uses custom error types to handle I/O and JSON parsing errors:
error_tree!
Usage
Save an Object to a File
To save an object to a file asynchronously, implement the SaveToFile
trait for the type:
let obj = YourStruct ;
obj.save_to_file.await?;
Load an Object from a File
To load an object from a file asynchronously, implement the LoadFromFile
trait for the type:
let obj = load_from_file.await?;
Load Objects from a Directory
To load multiple objects from a directory, use the LoadFromDirectory
trait:
let objects = load_from_directory.await?;
License
This crate is licensed under the MIT License. See LICENSE for details.