Hi, really often situation in our Rust projects is when we must save some data (eg. user preferences, configuration data) and read them in the next launch of our app. Of course, we could save them to matching files and nextly read them but there is a little bit code boilerplate. I front with this problem a few times so I wrote Configuer. With this lib, you can just create a struct that representing your data model, nextly you can manipulate data and save them. It's really simple. Just look at the code:
use configuer::Configuer;
use serde::{Deserialize, Serialize};
// Model must implement Serialize, Deserialize, CLone and Default. Debug is unneeded
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
struct MyData {
opens: usize,
}
fn main() {
let mut config = Configuer::<MyData>::with_file("myIniFileName");
println!("You opened my app {:?} times", config.data.opens);
config.data.opens += 1;
config.save();
}
As you can see, it's very simple. myIniFileName is the name of your config data. By default Configuer using dirs
crate and places the config file in the user home directory. You can change it through disable default features (then config file will be written just in your work dir).
You can set an on_create
hook.
use configuer::Configuer;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Default)]
struct MyData {
user_name: String,
}
fn main() {
let mut config = Configuer::with_file("myIniFileName").on_create(|| {
println!("I see you open this app very first time, please pass your name: ...");
MyData {
user_name: "Default user name".into(),
}
});
println!("{:?}", config.data);
config.save();
}
Now when the user opens our app for the first time, he will be asked about the name. Later when function returns data, it will be saved as default.
Tell me what do you think, and what could I add :D
https://crates.io/crates/configuer
https://github.com/Milesq/configuer
Top comments (0)