-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting config path via env variable
- Loading branch information
Showing
3 changed files
with
60 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,49 @@ | ||
use std::{fs, path::Path}; | ||
use std::{ | ||
env, | ||
fs::{self, File}, | ||
io::Write, | ||
path::PathBuf, | ||
}; | ||
|
||
use crate::profile::Profile; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{profile::Profile, util::StorageTrait}; | ||
|
||
#[derive(Debug, Deserialize, Serialize, Default)] | ||
pub struct Settings { | ||
pub profiles: Vec<Profile>, | ||
pub ui_state: Profile, | ||
// Up to 0.19.5 | ||
#[serde(alias = "ui_state")] | ||
pub current_profile: Profile, | ||
} | ||
|
||
impl Settings { | ||
/// Load the settings from the specified path or generate default ones if an error occurs | ||
pub fn load_or_default(path: &Path) -> Self { | ||
/// Load the settings from the configured path or generate default ones if an error occurs | ||
pub fn load() -> Self { | ||
let mut persist: Self = Self::default(); | ||
|
||
if let Ok(string) = fs::read_to_string(path) { | ||
if let Ok(string) = fs::read_to_string(Self::get_location()) { | ||
persist = serde_json::from_str(&string).unwrap_or_default(); | ||
} | ||
|
||
persist | ||
} | ||
} | ||
|
||
impl<'a> StorageTrait<'a> for Settings {} | ||
/// Save the settings to the configured path | ||
pub fn save(&mut self) { | ||
let mut file = File::create(Self::get_location()).unwrap(); | ||
|
||
let stringified_json = serde_json::to_string(&self).unwrap(); | ||
|
||
file.write_all(stringified_json.as_bytes()).unwrap(); | ||
} | ||
|
||
fn get_location() -> PathBuf { | ||
let default = PathBuf::from("./settings.json"); | ||
|
||
if let Ok(maybe_path) = env::var("LEGION_KEYBOARD_CONFIG") { | ||
PathBuf::try_from(maybe_path).unwrap_or(default) | ||
} else { | ||
default | ||
} | ||
} | ||
} |