31 stable releases (3 major)

5.0.2 Mar 10, 2023
5.0.1 Feb 26, 2023
4.15.0 Feb 15, 2023
2.1.1 Feb 9, 2023
1.1.1 Feb 6, 2023

#5 in #xtra

Download history 15/week @ 2024-07-24 4/week @ 2024-07-31 21/week @ 2024-09-18 21/week @ 2024-09-25

97 downloads per month
Used in 2 crates (via contra)

Custom license

17KB
166 lines

Contra

Publish Version Lint, Build and Test

Contra is a configuration file loader for Rust.

The serialization/deserialization is heavily "inspired" (if not blatantly copied) from the serde crate. Special thanks to 'Josh Mcguigan' and his very helpful article Understanding-serde.

Features

  • Load and save literals
    • string literals
    • numeric literals
    • enum literals
  • Load and save structs
    • primitive structs
    • nested structs
  • Load collections
    • vectors
    • maps
  • Support multiple File Formats
    • JSON
    • TOML
    • Cfg

Usuage

Contra adds the derive macro: Serialize which implements the serialize method for the given struct. Contra adds the derive macro: Deserialize which implements the deserialize method for the given struct. These functions are best used via the Persistent trait which automatically implemented for all struct that are both Serializable, and Deserializable. The Persistent trait provides the functions load and save, which selects the appropiate serializer/deserializer based on the path given as parameter.

Example

#[derive(Serialize, Deserialize, Default)]
pub struct Player {
    name: String,
    health: i32,
    dmg: f32,
    items: Vec<Item>,
}

#[derive(Deserialize, Deserialize)]
pub struct Item {
    name: String,
    slot: u32,
    stats: Vec<f32>,
}

fn main() {
    let saved_player = Player::default();
    player1::save("Player1.json").expect("failed to save player");

    let loaded_player = Player::load("Player1.json").expect("failed to load player");
}

Dependencies

~1.5MB
~37K SLoC