-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve global settings management (#1225)
- Loading branch information
1 parent
bbe26cc
commit f0941f8
Showing
4 changed files
with
111 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::fs::{self}; | ||
|
||
use std::env; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct GlobalSettings { | ||
pub enable_hints: Option<bool>, | ||
pub enable_telemetry: Option<bool>, | ||
} | ||
|
||
impl GlobalSettings { | ||
pub fn get_settings_file_path() -> &'static str { | ||
"~/.clarinet/clarinetrc.toml" | ||
} | ||
|
||
pub fn from_global_file() -> Self { | ||
let home_dir = dirs::home_dir(); | ||
|
||
if let Some(path) = home_dir.map(|home_dir| home_dir.join(".clarinet/clarinetrc.toml")) { | ||
if path.exists() { | ||
match fs::read_to_string(path) { | ||
Ok(content) => match toml::from_str::<GlobalSettings>(&content) { | ||
Ok(res) => return res, | ||
Err(_) => { | ||
println!( | ||
"{} {}", | ||
format_warn!("unable to parse"), | ||
Self::get_settings_file_path() | ||
); | ||
} | ||
}, | ||
Err(_) => { | ||
println!( | ||
"{} {}", | ||
format_warn!("unable to read file"), | ||
Self::get_settings_file_path() | ||
); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// Keep backwards compatibility with ENV var | ||
let enable_hints = match env::var("CLARINET_DISABLE_HINTS") { | ||
Ok(v) => Some(v == "1"), | ||
Err(_) => None, | ||
}; | ||
Self { | ||
enable_hints, | ||
..Default::default() | ||
} | ||
} | ||
} |
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,3 +1,5 @@ | ||
mod clarinetrc; | ||
|
||
pub mod cli; | ||
pub mod dap; | ||
#[cfg(feature = "telemetry")] | ||
|
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