From 211e3d619e82aeb73dc6c52868991384682869f5 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 26 Jun 2024 23:06:23 +0200 Subject: server auto-detect data dir --- server/src/data.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'server/src/data.rs') 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, } +pub static DATA_DIR: Mutex> = 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(); -- cgit v1.2.3-70-g09d2