Crates.io | conf-embed |
lib.rs | conf-embed |
version | 0.1.3 |
source | src |
created_at | 2019-05-18 16:49:54.592152 |
updated_at | 2019-05-20 21:36:26.962439 |
description | An experimental Rust crate for embedding user configuration (or anything really) in the executable of the program. |
homepage | |
repository | https://github.com/matrizx/conf-embed |
max_upload_size | |
id | 135110 |
size | 9,332 |
An experimental Rust crate for embedding user configuration (or anything really) in the executable of the program.
It pretty much does what the description says. It allows you to get and change some data that is stored in the executable of the program, meaning you don't need to use any additional files to store user configuration.
For example: To use this crate, you would first set a config (this can be any string slice but in this case I am using JSON data):
conf_embed::new_config("{\"arbitrary_data\": \"arbitrary_value\"").unwrap();
When this command is run, a new executable will be produced with the same name as the current one, and the current one will be renamed with .old
at the end of it.
For example, if the above code is ran on test.exe
, the current test.exe
will be renamed to test.exe.old
and test.exe
will become updated with the new config.
To get the config, simply use:
println!("Current config: '{}'", conf_embed::get_config().unwrap());
This works on all platforms, too.
Also, both functions above return an io::Result<()>
, meaning errors can be propagated like such:
use std::io;
use std::process;
fn main() -> io::Result<()> {
println!("Current config: '{}'", conf_embed::get_config()?);
println!("Enter a new config:");
let mut buf = String::new();
io::stdin().read_line(&mut buf)?;
println!("Setting new config...");
conf_embed::new_config(&buf)?;
println!("Config set, exiting.");
process::exit(0);
}
There are a few limitations you should know about when using this crate.
To begin, the string ----CONFIG-REF-START----
must not be anywhere in the set config. If it is, Err
will be returned.
Also, any sequence of 0's following the end of your config will be ignored. For example:
If your config is test test0000000
, this will be interpreted as test test
.
Finally, the config must be no more than 2024
bytes. This restriction is in place for safety and speed, but also to keep the binary size small.