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