about summary refs log tree commit diff
path: root/src/util/static_cfg.rs
diff options
context:
space:
mode:
authorGriffin Smith <root@gws.fyi>2019-07-20T01·55-0400
committerGriffin Smith <root@gws.fyi>2019-07-20T01·55-0400
commit29c80ac8ba0d733c6c452d8fd39e9561553495b0 (patch)
treef8384229d84b0d281c2fbbe976aeb5ebc21bdc24 /src/util/static_cfg.rs
parent4e9138aa6ff72e34392e3467c40d5ddf095f0027 (diff)
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
Diffstat (limited to 'src/util/static_cfg.rs')
-rw-r--r--src/util/static_cfg.rs58
1 files changed, 57 insertions, 1 deletions
diff --git a/src/util/static_cfg.rs b/src/util/static_cfg.rs
index 1b4864df72c9..b20456fb3bd4 100644
--- a/src/util/static_cfg.rs
+++ b/src/util/static_cfg.rs
@@ -14,6 +14,9 @@ macro_rules! __static_cfg_include {
     (json_dir, $filename:expr) => {
         include_dir!($filename)
     };
+    (cfg_dir, $filename:expr) => {
+        include_dir!($filename)
+    };
 }
 
 macro_rules! __static_cfg_type {
@@ -21,6 +24,7 @@ macro_rules! __static_cfg_type {
     (json_file) => (&'static str);
     (toml_dir) => (include_dir::Dir<'static>);
     (json_dir) => (include_dir::Dir<'static>);
+    (cfg_dir) => (include_dir::Dir<'static>);
 }
 
 macro_rules! __static_cfg_parse {
@@ -39,6 +43,10 @@ macro_rules! __static_cfg_parse {
     (json_dir, $e:expr) => {
         crate::util::static_cfg::parse_json_dir($e)
     };
+
+    (cfg_dir, $e:expr) => {
+        crate::util::static_cfg::parse_cfg_dir($e);
+    };
 }
 
 macro_rules! __static_cfg_inner {
@@ -70,13 +78,61 @@ macro_rules! static_cfg {
     () => ()
 }
 
+pub fn parse_cfg_dir<'a, T>(d: Dir<'a>) -> Vec<T>
+where
+    T: de::Deserialize<'a>,
+{
+    d.files()
+        .iter()
+        .filter_map(|f| {
+            let path = f.path();
+            let contents = f.contents_utf8().unwrap();
+            match path.extension().and_then(|e| e.to_str()) {
+                Some("toml") => {
+                    Some(toml::from_str(contents).unwrap_or_else(|e| {
+                        panic!(
+                            "Error parsing TOML file {}: {}",
+                            path.display(),
+                            e
+                        )
+                    }))
+                }
+                Some("json") => {
+                    Some(serde_json::from_str(contents).unwrap_or_else(|e| {
+                        panic!(
+                            "Error parsing JSON file {}: {}",
+                            path.display(),
+                            e
+                        )
+                    }))
+                }
+                // > YAML currently does not support zero-copy deserialization
+                // Some("yaml") => {
+                //     Some(serde_yaml::from_str(contents).unwrap_or_else(|e| {
+                //         panic!(
+                //             "Error parsing YAML file {}: {}",
+                //             path.display(),
+                //             e
+                //         )
+                //     }))
+                // }
+                _ => None,
+            }
+        })
+        .collect()
+}
+
 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())
+        .map(|f| {
+            toml::from_str(f.contents_utf8().unwrap()).unwrap_or_else(|e| {
+                panic!("Error parsing TOML file {}: {}", f.path, e)
+            })
+        })
         .collect()
 }