#preferences #settings #serde #config-file #json-format #options

abserde

Simple platform-agnostic Rust crate for managing application settings/preferences

11 unstable releases (5 breaking)

0.6.0 Jul 15, 2023
0.5.1 Jul 7, 2023
0.4.1 Aug 6, 2022
0.3.3 Aug 3, 2022
0.1.0 Aug 2, 2022

#284 in Configuration

Download history 22/week @ 2023-12-18 8/week @ 2024-01-22 11/week @ 2024-01-29 56/week @ 2024-02-19 42/week @ 2024-02-26 34/week @ 2024-03-04 35/week @ 2024-03-11 8/week @ 2024-03-18

120 downloads per month

MIT license

26KB
495 lines

abserde

Crates.io Crates.io Crates.io GitHub Workflow Status docs.rs

Simple platform-agnostic Rust crate for managing application settings/preferences.

Installation

Install the crate as a dependency in your app's Cargo.toml file:

[dependencies]
abserde = "0.6.0"

Usage

Import Abserde, associated definitions, and serde::Serialize, and serde::Deserialize:

use abserde::*;
use serde::{Serialize, Deserialize};

Define a struct to store your app config. You must derive your struct from serde::Serialize and serde::Deserialize traits.

use std::collections::HashMap;

#[derive(Serialize, Deserialize)]
struct MyConfig {
	window_width: usize,
	window_height: usize,
	window_x: usize,
	window_y: usize,
	theme: String,
	user_data: HashMap<String, String>,
}

Create an Abserde instance to manage how your configuration is stored on disk:

let my_abserde = Abserde::default();

Using Abserde in this way will use your crate as the name for the app config directory.

Alternatively, you can also pass options to Abserde to change the location or format of your config file:

let my_abserde = Abserde {
	app: "MyApp".to_string(),
	location: Location::Auto,
	format: Format::Json,
};

//! For the JSON format, you can pretty-print your config file, using either tabs or spaces:

let my_abserde = Abserde {
	app: "MyApp".to_string(),
	location: Location::Auto,
	format: Format::PrettyJson(PrettyJsonIndent::Tab),
};
let my_abserde = Abserde {
	app: "MyApp".to_string(),
	location: Location::Auto,
	format: Format::PrettyJson(PrettyJsonIndent::Spaces(4)),
};

Load data into config from disk:

let my_config = MyConfig::load_config(&my_abserde)?;

Save config data to disk:

my_config.save_config(&my_abserde)?;

Delete config file from disk:

my_abserde.delete()?;

Dependencies

~0.8–2.5MB
~47K SLoC