disk 0.0.1

(De)serialize files to/from disk
Documentation

Disk

Build crates.io docs.rs

Disk: serde + directories + a whole bunch of file formats as Traits.

This crate is for (de)serializing to/from various file formats (provided by serde) to/from disk locations that follow OS-specific specifications/conventions (provided by directories).

All errors returned are of type anyhow::Error.

File Formats

File Format Feature flag to enable
Bincode bincode
JSON json
TOML toml
YAML yaml
Pickle pickle
MessagePack messagepack
BSON bson
Plain Text plain

Example

Defining our struct, State:

use disk::prelude::*;       // Necessary imports to get things working.
use disk::{Toml,toml_file}; // <- TOML trait & macro.
use serde::{Serialize, Deserialize};

// To make this struct a file, use the following macro:
//
// |- 1. The file format used will be TOML.
// |
// |          |- 2. The struct "State" will be used.
// |          |
// |          |      |- 3. It will be saved in the OS Data directory.
// |          |      |
// |          |      |          |- 4. The main project directory is called "MyProject".
// |          |      |          |
// |          |      |          |            |- 6. It won't be in any sub-directories.
// |          |      |          |            |
// |          |      |          |            |   |- 7. The file name will be "state.toml".
// v          v      v          v            v   v
   toml_file!(State, Dir::Data, "MyProject", "", "state");
#[derive(Serialize,Deserialize)] // <- Your data must implement `serde`.
struct State {
    string: String,
    number: u32,
}

Saving State to disk:

let state = State { string: "Hello".to_string(), number: 0 };

// This writes to `~/.local/share/myproject/state.toml`
state.write().unwrap();

Creating a State from disk:

// This reads from `~/.local/share/myproject/state.toml`
let state = State::from_file().unwrap();