diff --git a/Cargo.toml b/Cargo.toml index 191fd43..576545a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,10 @@ crate-type = ["cdylib", "rlib"] [features] default = ["console_error_panic_hook", "wee_alloc"] +[build-dependencies] +serde = "1.0" +serde_json = "1.0" + [dependencies] cfg-if = "0.1.2" chrono = "0.4" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..40aee0c --- /dev/null +++ b/build.rs @@ -0,0 +1,19 @@ +extern crate serde_json; + +use std::io::prelude::*; + +fn main() { + println!("cargo:rerun-if-changed=config.json"); + // Load theme name from config.json and output code to load the theme via include_dir! + let config: serde_json::Value = + serde_json::from_str(&std::fs::read_to_string("./config.json").unwrap()).unwrap(); + let theme_name = match config.get("theme") { + Some(name) => name, + None => panic!("Please define `theme` in `config.json`") + }; + let theme_load_code = format!("const THEME_DIR: Dir = include_dir!(\"theme/{}\");", theme_name.as_str().unwrap()); + let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); + let mut out_file = std::fs::File::create(out_path.join("load_theme.rs")).unwrap(); + out_file.write_all(theme_load_code.as_bytes()).unwrap(); + out_file.sync_data().unwrap(); +} \ No newline at end of file diff --git a/src/render.rs b/src/render.rs index 1125ee1..86c9201 100644 --- a/src/render.rs +++ b/src/render.rs @@ -11,8 +11,9 @@ use serde::Serialize; use std::vec::Vec; use web_sys::*; -// TODO: allow static configuration of which theme to use -const THEME_DIR: Dir = include_dir!("theme/default"); +// Allows user-configurable theme at build-time +// See build.rs +include!(concat!(env!("OUT_DIR"), "/load_theme.rs")); pub fn build_routes(router: &mut Router) { router.add_route("/static/", &serve_static);