From 8e50eabaddb292960799af34689b29bd0207bd91 Mon Sep 17 00:00:00 2001 From: David Gay Date: Mon, 2 Oct 2023 01:14:56 -0400 Subject: Use include_dir to include all yaml files --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 1 + src/random_tables.rs | 11 +++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c0bb18..dd58246 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,7 @@ name = "dmn" version = "0.1.0" dependencies = [ "clap", + "include_dir", "rand", "serde", "serde_yaml", @@ -122,6 +123,25 @@ version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" +[[package]] +name = "include_dir" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "2.0.2" diff --git a/Cargo.toml b/Cargo.toml index 345e4bd..02728aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ repository = "https://git.aikuro.net/poems/dmn" [dependencies] clap = { version = "^4.4", features = ["cargo"] } +include_dir = "0.7.3" rand = "^0.8" serde = { version = "^1.0", features = ["derive"] } serde_yaml = "^0.9" diff --git a/src/random_tables.rs b/src/random_tables.rs index 1d9df02..2428ad2 100644 --- a/src/random_tables.rs +++ b/src/random_tables.rs @@ -3,6 +3,7 @@ use serde_yaml; use std::collections::HashMap; use std::error::Error; use std::string::String; +use include_dir::{include_dir, Dir}; use crate::dice; #[derive(Debug, Deserialize)] @@ -27,10 +28,16 @@ pub struct RandomTables { tables: HashMap, } +const YAML_DIR : Dir = include_dir!("src/data/random_tables/"); + impl RandomTables { pub fn new() -> Result> { - let tables_yaml = include_str!("data/random_tables/ua_magic_items.yaml"); - let tables: HashMap = serde_yaml::from_str(tables_yaml)?; + let mut tables_yaml = String::new(); + for entry in YAML_DIR.files() { + tables_yaml.push_str(entry.contents_utf8().unwrap()); + } + + let tables: HashMap = serde_yaml::from_str(tables_yaml.as_str())?; Ok(RandomTables { tables }) } -- cgit v1.2.3