From 29c80ac8ba0d733c6c452d8fd39e9561553495b0 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Fri, 19 Jul 2019 21:55:09 -0400 Subject: Add the beginning of item entities Add a new Item raw type and entity type, with preliminary, basic support for food. There's a really frustrating toml-rs bug that prevents writing these nicely as toml right now, so I also added support for mixing JSON and TOML in a single config dir --- src/messages.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/messages.rs') diff --git a/src/messages.rs b/src/messages.rs index 9ca78025ec60..719389fa6136 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; #[derive(Deserialize, Debug, PartialEq, Eq)] #[serde(untagged)] -enum Message<'a> { +pub enum Message<'a> { #[serde(borrow)] Single(Template<'a>), Choice(Vec>), @@ -123,6 +123,13 @@ static_cfg! { static ref MESSAGES: NestedMap<'static> = toml_file("messages.toml"); } +pub fn get( + name: &'static str, + rng: &mut R, +) -> Option<&'static Template<'static>> { + MESSAGES.lookup(name).and_then(|msg| msg.resolve(rng)) +} + /// Look up and format a game message based on the given (dot-separated) name, /// with the given random generator used to select from choice-based messages pub fn message<'a, R: Rng + ?Sized>( @@ -130,7 +137,7 @@ pub fn message<'a, R: Rng + ?Sized>( rng: &mut R, params: &TemplateParams<'a>, ) -> String { - match MESSAGES.lookup(name).and_then(|msg| msg.resolve(rng)) { + match get(name, rng) { Some(msg) => msg.format(params).unwrap_or_else(|e| { error!("Error formatting template: {}", e); "Template Error".to_string() -- cgit 1.4.1