diff options
author | Griffin Smith <root@gws.fyi> | 2019-07-14T18·29-0400 |
---|---|---|
committer | Griffin Smith <root@gws.fyi> | 2019-07-14T18·29-0400 |
commit | 081146da30bcf1a17d9533c3dc9c735a3a558165 (patch) | |
tree | cfccab3ec7f2346897adc8d50c872d49e3b45fd3 /src | |
parent | 67d18b486c6376c7637b3494722ddf1eb525288c (diff) |
Allow static_cfg to include entire directories
Via new "toml_dir" and "json_dir" directives in the macro
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/util/mod.rs | 2 | ||||
-rw-r--r-- | src/util/static_cfg.rs | 91 | ||||
-rw-r--r-- | src/util/static_toml.rs | 37 |
4 files changed, 96 insertions, 40 deletions
diff --git a/src/main.rs b/src/main.rs index e09563b16cc7..f7257a889653 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 e26eae2b89b3..87fd7910f3ec 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 000000000000..1b4864df72c9 --- /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<T> +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<T> +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 7a930ee023c8..000000000000 --- 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)*); - }; - - () => () -} |