diff options
Diffstat (limited to 'server/src/data.rs')
-rw-r--r-- | server/src/data.rs | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/server/src/data.rs b/server/src/data.rs index e980ccbd..e2944e3d 100644 --- a/server/src/data.rs +++ b/server/src/data.rs @@ -25,7 +25,9 @@ use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, fs::File, - sync::RwLock, + path::PathBuf, + str::FromStr, + sync::{Mutex, RwLock}, }; #[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)] @@ -104,9 +106,18 @@ pub struct DataIndex { pub recipes: HashSet<String>, } +pub static DATA_DIR: Mutex<Option<PathBuf>> = Mutex::new(None); +fn data_dir() -> PathBuf { + DATA_DIR + .lock() + .unwrap() + .to_owned() + .unwrap_or_else(|| PathBuf::from_str("data").unwrap()) +} + impl DataIndex { pub fn reload(&mut self) -> anyhow::Result<()> { - *self = serde_yaml::from_reader(File::open("data/index.yaml")?)?; + *self = serde_yaml::from_reader(File::open(data_dir().join("index.yaml"))?)?; Ok(()) } @@ -127,9 +138,9 @@ impl DataIndex { bail!("unknown recipes: {recipes:?}"); } - let demands_path = format!("data/demands/{demands}.yaml"); - let map_path = format!("data/maps/{map}.yaml"); - let recipes_path = format!("data/recipes/{recipes}.yaml"); + let demands_path = data_dir().join(format!("demands/{demands}.yaml")); + let map_path = data_dir().join(format!("maps/{map}.yaml")); + let recipes_path = data_dir().join(format!("recipes/{recipes}.yaml")); let demands_in = serde_yaml::from_reader(File::open(demands_path).unwrap()).unwrap(); let map_in = serde_yaml::from_reader(File::open(map_path).unwrap()).unwrap(); |