4 releases
0.1.3 | May 20, 2019 |
---|---|
0.1.2 | May 18, 2019 |
0.1.1 | May 18, 2019 |
0.1.0 | May 18, 2019 |
#556 in Configuration
10KB
75 lines
conf-embed
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.
NOTE: This crate is semi-stable, but due to the nature of what it is doing, it is also unsafe. Use with caution.
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.