From 081146da30bcf1a17d9533c3dc9c735a3a558165 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 14 Jul 2019 14:29:11 -0400 Subject: [PATCH] Allow static_cfg to include entire directories Via new "toml_dir" and "json_dir" directives in the macro --- Cargo.lock | 58 ++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 6 ++- src/util/mod.rs | 2 +- src/util/static_cfg.rs | 91 +++++++++++++++++++++++++++++++++++++++++ src/util/static_toml.rs | 37 ----------------- 6 files changed, 156 insertions(+), 40 deletions(-) create mode 100644 src/util/static_cfg.rs delete mode 100644 src/util/static_toml.rs diff --git a/Cargo.lock b/Cargo.lock index 3b523d247..b0020c466 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -319,6 +319,11 @@ dependencies = [ "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "humantime" version = "1.2.0" @@ -327,6 +332,28 @@ dependencies = [ "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "include_dir" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "include_dir_impl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "include_dir_impl" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itertools" version = "0.8.0" @@ -521,6 +548,19 @@ dependencies = [ "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro-hack" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-hack-impl" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "0.4.30" @@ -899,6 +939,16 @@ name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "syn" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.15.35" @@ -1092,6 +1142,7 @@ dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "config 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "downcast-rs 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "include_dir 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1103,6 +1154,7 @@ dependencies = [ "rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1162,7 +1214,10 @@ dependencies = [ "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum getrandom 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e65cce4e5084b14874c4e7097f38cab54f47ee554f9194673456ea379dcc4c55" +"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" +"checksum include_dir 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f41a8bee1894b3fb755d8f09ccd764650476358197a0582555f698fe84b0ae93" +"checksum include_dir_impl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4b029199aef0fb9921fdc5623843197e6f4a035774523817599a9f55e4bf3b" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" @@ -1187,6 +1242,8 @@ dependencies = [ "checksum ordered-float 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "18869315e81473c951eb56ad5558bbc56978562d3ecfb87abb7a1e944cea4518" "checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b" "checksum prettytable-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0fd04b170004fa2daccf418a7f8253aaf033c27760b5f225889024cf66d7ac2e" +"checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8" +"checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proptest 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2afed8cbdc8a64b58a5c021757a782351ec1afee85be374872721c84d5da5d80" "checksum proptest-derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08b264c54525e760fc1d39c5b2bfc96923b922a752893053b4adaafe33fa9346" @@ -1231,6 +1288,7 @@ dependencies = [ "checksum serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)" = "38b08a9a90e5260fe01c6480ec7c811606df6d3a660415808c3c3fa8ed95b582" "checksum spin 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44363f6f51401c34e7be73db0db371c04705d35efbe9f7d6082e03a921a32c55" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +"checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741" "checksum syn 0.15.35 (registry+https://github.com/rust-lang/crates.io-index)" = "641e117d55514d6d918490e47102f7e08d096fdde360247e4a10f7a91a8478d3" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" "checksum tempfile 3.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7dc4738f2e68ed2855de5ac9cdbe05c9216773ecde4739b2f095002ab03a13ef" diff --git a/Cargo.toml b/Cargo.toml index 58fd93d3f..02464410e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ backtrace = "0.3" clap = {version = "^2.33.0", features = ["yaml"]} config = "*" downcast-rs = "^1.0.4" +include_dir = "0.2.1" itertools = "*" lazy_static = "*" log = "*" @@ -20,6 +21,7 @@ proptest-derive = "*" rand = {version = "^0.7.0", features = ["small_rng"]} serde = "^1.0.8" serde_derive = "^1.0.8" +serde_json = "*" termion = "*" toml = "^0.5.1" diff --git a/src/main.rs b/src/main.rs index e09563b16..f7257a889 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,14 +19,16 @@ extern crate maplit; #[macro_use] extern crate downcast_rs; extern crate backtrace; +#[macro_use] +extern crate include_dir; +#[macro_use] +mod util; mod display; mod game; #[macro_use] mod types; mod entities; -#[macro_use] -mod util; mod messages; mod settings; diff --git a/src/util/mod.rs b/src/util/mod.rs index e26eae2b8..87fd7910f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,2 +1,2 @@ #[macro_use] -pub mod static_toml; +pub mod static_cfg; diff --git a/src/util/static_cfg.rs b/src/util/static_cfg.rs new file mode 100644 index 000000000..1b4864df7 --- /dev/null +++ b/src/util/static_cfg.rs @@ -0,0 +1,91 @@ +use include_dir::Dir; +use serde::de; + +macro_rules! __static_cfg_include { + (toml_file, $filename:expr) => { + include_str!($filename) + }; + (toml_dir, $filename:expr) => { + include_dir!($filename) + }; + (json_file, $filename:expr) => { + include_str!($filename) + }; + (json_dir, $filename:expr) => { + include_dir!($filename) + }; +} + +macro_rules! __static_cfg_type { + (toml_file) => (&'static str); + (json_file) => (&'static str); + (toml_dir) => (include_dir::Dir<'static>); + (json_dir) => (include_dir::Dir<'static>); +} + +macro_rules! __static_cfg_parse { + (toml_file, $e:expr) => { + toml::from_str($e).unwrap() + }; + + (json_file, $e:expr) => { + serde_json::from_str($e).unwrap() + }; + + (toml_dir, $e:expr) => { + crate::util::static_cfg::parse_toml_dir($e) + }; + + (json_dir, $e:expr) => { + crate::util::static_cfg::parse_json_dir($e) + }; +} + +macro_rules! __static_cfg_inner { + ($(#[$attr:meta])* ($($vis:tt)*) static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { + // static RAW: &'static str = __static_cfg_include!($kind, $filename); + static RAW: __static_cfg_type!($kind) = __static_cfg_include!($kind, $filename); + lazy_static! { + $(#[$attr])* static ref $N: $T = __static_cfg_parse!($kind, RAW); + } + + static_cfg!($($t)*); + } +} + +#[macro_export] +macro_rules! static_cfg { + ($(#[$attr:meta])* static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { + __static_cfg_inner!($(#[$attr])* () static ref $N : $T = $kind($filename); $($t)*); + }; + + ($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { + __static_cfg_inner!($(#[$attr])* (pub) static ref $N : $T = $kind($filename); $($t)*); + }; + + ($(#[$attr:meta])* pub ($($vis:tt)+) static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { + __static_cfg_inner!($(#[$attr])* (pub ($($vis)+)) static ref $N : $T = $kind($filename); $($t)*); + }; + + () => () +} + +pub fn parse_toml_dir<'a, T>(d: Dir<'a>) -> Vec +where + T: de::Deserialize<'a>, +{ + d.files() + .iter() + .map(|f| toml::from_str(f.contents_utf8().unwrap()).unwrap()) + .collect() +} + +pub fn parse_json_dir<'a, T>(d: Dir<'a>) -> Vec +where + T: de::Deserialize<'a>, +{ + d.files() + .iter() + .map(|f| serde_json::from_str(f.contents_utf8().unwrap()).unwrap()) + .collect() +} diff --git a/src/util/static_toml.rs b/src/util/static_toml.rs deleted file mode 100644 index 7a930ee02..000000000 --- a/src/util/static_toml.rs +++ /dev/null @@ -1,37 +0,0 @@ -macro_rules! __static_cfg_parse { - (toml_file, $e:expr) => { - toml::from_str($e) - }; - - (json_file, $e:expr) => { - json::from_str($e) - }; -} - -macro_rules! __static_cfg_inner { - ($(#[$attr:meta])* ($($vis:tt)*) static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { - static RAW: &'static str = include_str!($filename); - lazy_static! { - $(#[$attr])* static ref $N: $T = __static_cfg_parse!($kind, RAW).unwrap(); - } - - static_cfg!($($t)*); - } -} - -#[macro_export] -macro_rules! static_cfg { - ($(#[$attr:meta])* static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { - __static_cfg_inner!($(#[$attr])* () static ref $N : $T = $kind($filename); $($t)*); - }; - - ($(#[$attr:meta])* pub static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { - __static_cfg_inner!($(#[$attr])* (pub) static ref $N : $T = $kind($filename); $($t)*); - }; - - ($(#[$attr:meta])* pub ($($vis:tt)+) static ref $N:ident : $T:ty = $kind:ident($filename:expr); $($t:tt)*) => { - __static_cfg_inner!($(#[$attr])* (pub ($($vis)+)) static ref $N : $T = $kind($filename); $($t)*); - }; - - () => () -}