44 lines
1.2 KiB
Rust
44 lines
1.2 KiB
Rust
use std::{
|
|
collections::HashSet,
|
|
fs::File,
|
|
io::{self, Read, Write},
|
|
};
|
|
|
|
use crate::language::DebugAsk;
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
pub struct ReplOptions {
|
|
pub debug_asks: HashSet<DebugAsk>,
|
|
pub show_total_time: bool,
|
|
pub show_stage_times: bool,
|
|
}
|
|
|
|
impl ReplOptions {
|
|
pub fn new() -> ReplOptions {
|
|
ReplOptions { debug_asks: HashSet::new(), show_total_time: true, show_stage_times: false }
|
|
}
|
|
|
|
pub fn save_to_file(&self, filename: &str) {
|
|
let res = File::create(filename).and_then(|mut file| {
|
|
let buf = crate::serde_json::to_string(self).unwrap();
|
|
file.write_all(buf.as_bytes())
|
|
});
|
|
if let Err(err) = res {
|
|
eprintln!("Error saving {} file {}", filename, err);
|
|
}
|
|
}
|
|
|
|
pub fn load_from_file(filename: &str) -> Result<ReplOptions, io::Error> {
|
|
File::open(filename)
|
|
.and_then(|mut file| {
|
|
let mut contents = String::new();
|
|
file.read_to_string(&mut contents)?;
|
|
Ok(contents)
|
|
})
|
|
.and_then(|contents| {
|
|
let output: ReplOptions = crate::serde_json::from_str(&contents)?;
|
|
Ok(output)
|
|
})
|
|
}
|
|
}
|