diff options
Diffstat (limited to '')
| -rw-r--r-- | src/config/loader.rs | 70 | ||||
| -rw-r--r-- | src/config/mod.rs | 3 |
2 files changed, 73 insertions, 0 deletions
diff --git a/src/config/loader.rs b/src/config/loader.rs new file mode 100644 index 0000000..81a4351 --- /dev/null +++ b/src/config/loader.rs | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | use std::{env, path::PathBuf}; | ||
| 2 | |||
| 3 | use serde::Deserialize; | ||
| 4 | |||
| 5 | use crate::{constants::*, errors::McError}; | ||
| 6 | |||
| 7 | #[allow(dead_code)] | ||
| 8 | #[derive(Debug, Deserialize)] | ||
| 9 | pub struct Config { | ||
| 10 | pub username: String, | ||
| 11 | pub uuid: String, | ||
| 12 | pub version: String, | ||
| 13 | pub java_path: String, | ||
| 14 | pub max_memory_mb: u32, | ||
| 15 | pub data_dir: PathBuf, | ||
| 16 | pub cache_dir: PathBuf, | ||
| 17 | pub config_dir: PathBuf, | ||
| 18 | #[serde(default)] | ||
| 19 | pub jvm_args: Vec<String>, | ||
| 20 | } | ||
| 21 | |||
| 22 | impl Config { | ||
| 23 | pub fn load() -> Result<Self, McError> { | ||
| 24 | let cfg_path = default_config_path()?; | ||
| 25 | let mut cfg: Config = if cfg_path.exists() { | ||
| 26 | let txt = std::fs::read_to_string(&cfg_path)?; | ||
| 27 | toml::from_str(&txt).map_err(|e| McError::Config(e.to_string()))? | ||
| 28 | } else { | ||
| 29 | Self::default() | ||
| 30 | }; | ||
| 31 | |||
| 32 | if let Ok(v) = env::var("MC_USERNAME") { | ||
| 33 | cfg.username = v; | ||
| 34 | } | ||
| 35 | if let Ok(v) = env::var("MC_VERSION") { | ||
| 36 | cfg.version = v; | ||
| 37 | } | ||
| 38 | if let Ok(v) = env::var("MC_JAVA_PATH") { | ||
| 39 | cfg.java_path = v; | ||
| 40 | } | ||
| 41 | if let Ok(v) = env::var("MC_MAX_MEMORY_MB") { | ||
| 42 | cfg.max_memory_mb = v.parse().unwrap_or(cfg.max_memory_mb); | ||
| 43 | } | ||
| 44 | |||
| 45 | Ok(cfg) | ||
| 46 | } | ||
| 47 | |||
| 48 | fn default() -> Self { | ||
| 49 | let base = | ||
| 50 | directories::ProjectDirs::from("com", "example", "mccl").expect("platform dirs"); | ||
| 51 | |||
| 52 | Self { | ||
| 53 | username: "Player".into(), | ||
| 54 | uuid: uuid::Uuid::new_v4().to_string(), | ||
| 55 | version: DEFAULT_VERSION.into(), | ||
| 56 | java_path: DEFAULT_JAVA_PATH.into(), | ||
| 57 | max_memory_mb: DEFAULT_MAX_MEMORY_MB, | ||
| 58 | data_dir: base.data_dir().into(), | ||
| 59 | cache_dir: base.cache_dir().into(), | ||
| 60 | config_dir: base.config_dir().into(), | ||
| 61 | jvm_args: vec![], | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | fn default_config_path() -> Result<PathBuf, McError> { | ||
| 67 | let base = directories::ProjectDirs::from("com", "example", "mccl") | ||
| 68 | .ok_or_else(|| McError::Config("cannot determine config dir".into()))?; | ||
| 69 | Ok(base.config_dir().join("config.toml")) | ||
| 70 | } | ||
diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..c5fc004 --- /dev/null +++ b/src/config/mod.rs | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | pub mod loader; | ||
| 2 | |||
| 3 | pub use loader::Config; | ||
