diff options
author | Griffin Smith <root@gws.fyi> | 2019-07-14T16·12-0400 |
---|---|---|
committer | Griffin Smith <root@gws.fyi> | 2019-07-14T16·12-0400 |
commit | 67d18b486c6376c7637b3494722ddf1eb525288c (patch) | |
tree | a2c7c415e685a00ca005df9ff643c162f772d9a6 /src/util | |
parent | 405dbffe376b05af31dc57f027658c70b4fb9634 (diff) |
Factor out static_cfg from static init of messages
Factor out a macro for static references to data parsed from config files at compile-time.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/mod.rs | 2 | ||||
-rw-r--r-- | src/util/static_toml.rs | 37 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/util/mod.rs b/src/util/mod.rs index e69de29bb2d1..e26eae2b89b3 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -0,0 +1,2 @@ +#[macro_use] +pub mod static_toml; diff --git a/src/util/static_toml.rs b/src/util/static_toml.rs new file mode 100644 index 000000000000..7a930ee023c8 --- /dev/null +++ b/src/util/static_toml.rs @@ -0,0 +1,37 @@ +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)*); + }; + + () => () +} |