Crate crio

source · []
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. The underlying data is stored in a .crf file on disk. Since all the data is loaded onto memory when calling load on a Client, 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

Examples

#[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 path = PathBuf::new("messages.crf");
let client: Client<Message> = Client::new(path)?; // 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

This client does the same thing as Client<T> with a few notable differences. The write() method merely appends data to an existing file if it exists instead of overwriting the data. There are also two write methods: write_many() which accepts a vector of objects, and write(), which only accepts a single object.

An object that is responsible for handling IO operations with regards to file opening/closing/writing as well as serialization and deserialization.

This errors occurs due to a checksum mismatch. Thus it is important to backup your .crf 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.