Expand description
This crate provides an easy to use API to store persistent data of the same type.
Any type that is able to be deserialized or serialized using Serde can be stored on disk.
Data is stored on disk with a CRC32 checksum associated with every object stored to ensure
data integrity. In the event of a checksum mismatch, this API returns a DataPoisonError<T>,
similar to the concept of poisoning in mutexes, in which data stored on disk might be in a
bad state and probably should not be used. However, like PoisonError in std, the API provides
you with methods to get the underlying value if you really need it.
This crate is meant for storing small serializable data that stores the state of an application
after exit. Since all the data is loaded
onto memory when calling load on a Client<T>, handling large amounts of data is not advised. However,
the data stored on disk has a relatively small footprint and should not take up that much space.
Note that this is not an embedded database and there are other libraries which are better suited
for this task, such as sled:
https://github.com/spacejam/sled
This library is once primarily for my own personal use. I simply decided to open-source it just in case anyone finds this useful. There are no file headers or data versioning support in the created file, but the format of the data should remain stable for the forseeable future.
Example
#[derive(Serialize, Deserialize, Debug)]
struct Message {
id: usize,
message: String,
}
let msg1 = Message {
id: 1,
message: "Hello there, you suck".to_string(),
};
let msg2 = Message {
id: 2,
message: "No you".to_string(),
};
let msg3 = Message {
id: 3,
message: "You both suck".to_string(),
};
let messages = vec![msg1, msg2, msg3];
let client: Client<Message> = Client::new("messages", false)?; // If no file is found, a new empty file is created
client.write(messages)?; // If no file is found, a new file is created and then written to
let messages = client.load()?;
if let Some(data) = messages {
println!("Here are your messages: {:?}", data);
} else {
panic!("File is empty");
}Structs
An object that is responsible for handling IO operations with regards to file opening/closing/writing as well as serialization and deserialization. The main data type of this crate.
This error occurs due to a checksum mismatch. Therefore it is important to backup your
files periodically to prevent data loss. However, you can still get the underlying
objects if you are sure only one or two objects are malformed via the into_inner() method
or its equivalents, in which case count your lucky stars as serde is still able to
deserialize your objects, or that the saved checksum is the one that is corrupted instead
of your objects.
Enums
This is the main error type of this crate.