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.
This commit is contained in:
parent
405dbffe37
commit
67d18b486c
4 changed files with 45 additions and 12 deletions
11
src/main.rs
11
src/main.rs
|
@ -25,9 +25,10 @@ mod game;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod types;
|
mod types;
|
||||||
mod entities;
|
mod entities;
|
||||||
|
#[macro_use]
|
||||||
|
mod util;
|
||||||
mod messages;
|
mod messages;
|
||||||
mod settings;
|
mod settings;
|
||||||
mod util;
|
|
||||||
|
|
||||||
use clap::App;
|
use clap::App;
|
||||||
use game::Game;
|
use game::Game;
|
||||||
|
@ -49,13 +50,9 @@ fn init(
|
||||||
h: u16,
|
h: u16,
|
||||||
) {
|
) {
|
||||||
panic::set_hook(if settings.logging.print_backtrace {
|
panic::set_hook(if settings.logging.print_backtrace {
|
||||||
Box::new(|info| {
|
Box::new(|info| (error!("{}\n{:#?}", info, Backtrace::new())))
|
||||||
(error!("{}\n{:#?}", info, Backtrace::new()))
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
Box::new(|info| {
|
Box::new(|info| (error!("{}\n{:#?}", info, Backtrace::new())))
|
||||||
(error!("{}\n{:#?}", info, Backtrace::new()))
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let game = Game::new(settings, stdout, stdin, w, h);
|
let game = Game::new(settings, stdout, stdin, w, h);
|
||||||
|
|
|
@ -165,11 +165,8 @@ choice = ["Say this", "Or this"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static MESSAGES_RAW: &'static str = include_str!("messages.toml");
|
static_cfg! {
|
||||||
|
static ref MESSAGES: NestedMap<'static> = toml_file("messages.toml");
|
||||||
lazy_static! {
|
|
||||||
static ref MESSAGES: NestedMap<'static> =
|
|
||||||
toml::from_str(MESSAGES_RAW).unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Look up a game message based on the given (dot-separated) name, with the
|
/// Look up a game message based on the given (dot-separated) name, with the
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
#[macro_use]
|
||||||
|
pub mod static_toml;
|
37
src/util/static_toml.rs
Normal file
37
src/util/static_toml.rs
Normal file
|
@ -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)*);
|
||||||
|
};
|
||||||
|
|
||||||
|
() => ()
|
||||||
|
}
|
Loading…
Reference in a new issue