This library is designed to be a dead-simple way of reading and writing your rust values to and from disk. It greatly reduces the boilerplate necessary for using simple config files or basic JSON datastores.
See the docs for each crate for more info:
Crate name | Crates.io page | Docs.rs page |
---|---|---|
singlefile |
||
singlefile-formats |
// A JSON file format, utilizing serde
use singlefile_formats::json_serde::Json;
// A readable, writable container
use singlefile::container::ContainerWritable;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Default)]
struct MyData {
magic_number: i32
}
// Attempts to open 'my_data.json', creating it from default if it does not exist,
// expecting data that the `Json` format can decode into `MyData`
let mut my_container = ContainerWritable::<MyData, Json>::create_or_default("my_data.json", Json)?;
// For regular `Container`s, `Deref` and `DerefMut` can be used to access the contained type
println!("magic_number: {}", my_container.magic_number); // 0 (as long as the file didn't exist before)
my_container.magic_number += 1;
// Write the new state of `MyData` to disk
my_container.commit()?;