Skip to content

Instantly share code, notes, and snippets.

@NBZ4live
Created February 16, 2020 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NBZ4live/329e4a3bb24d2785e3d7a247a233d644 to your computer and use it in GitHub Desktop.
Save NBZ4live/329e4a3bb24d2785e3d7a247a233d644 to your computer and use it in GitHub Desktop.
Rust config_struct crate build script with check for the existence of the config file
use config_struct::{Error, StructOptions};
use std::fs;
const CONFIG_FILE_DST: &str = "src/config.rs";
fn main() -> Result<(), Error> {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=config.toml");
let mut config_file_src = "config.toml";
let metadata = fs::metadata(config_file_src);
if metadata.is_err() || !metadata.unwrap().is_file() {
println!("cargo:warning={} not found!", config_file_src);
config_file_src = "config-example.toml";
println!("cargo:warning=Using {} instead!", config_file_src);
}
config_struct::create_config(
config_file_src,
CONFIG_FILE_DST,
&StructOptions::default()
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment