-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for storing settings on a per-profile and global basis
- Loading branch information
Showing
2 changed files
with
160 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::path::PathBuf; | ||
use std::collections::HashMap; | ||
use serde_json::Value; | ||
use std::fs::OpenOptions; | ||
use std::io; | ||
|
||
// === GLOBAL OPTIONS === | ||
|
||
//// Read global options from the specified file | ||
pub fn read_global_options(path: &PathBuf) -> HashMap<String, Value> { | ||
if let Ok(file) = OpenOptions::new().read(true).open(path) { | ||
if let Ok(options) = serde_json::from_reader(file) { | ||
return options; | ||
} | ||
} | ||
|
||
// Global options don't exist or is invalid, load empty options | ||
HashMap::new() | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum WriteGlobalOptionsError { | ||
OpenFileError(io::Error), | ||
WriteFileError(serde_json::Error) | ||
} | ||
|
||
//// Read global options to the specified file | ||
pub fn write_global_options(path: &PathBuf, new_options: &HashMap<String, Value>) -> Result<(), WriteGlobalOptionsError> { | ||
let options_file = OpenOptions::new() | ||
.create(true) | ||
.truncate(true) | ||
.write(true) | ||
.open(path) | ||
.map_err(WriteGlobalOptionsError::OpenFileError)?; | ||
|
||
serde_json::to_writer(options_file, &new_options) | ||
.map_err(WriteGlobalOptionsError::WriteFileError) | ||
} |