schala/schala-repl/src/repl_options.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

2021-11-24 23:51:30 -08:00
use std::{
collections::HashSet,
fs::File,
io::{self, Read, Write},
};
2019-05-14 00:40:38 -07:00
2021-11-24 23:51:30 -08:00
use crate::language::DebugAsk;
2019-05-14 00:40:38 -07:00
#[derive(Serialize, Deserialize)]
2019-05-21 02:46:07 -07:00
pub struct ReplOptions {
2021-10-07 01:19:35 -07:00
pub debug_asks: HashSet<DebugAsk>,
pub show_total_time: bool,
pub show_stage_times: bool,
2019-05-14 00:40:38 -07:00
}
2019-05-21 02:46:07 -07:00
impl ReplOptions {
2021-10-07 01:19:35 -07:00
pub fn new() -> ReplOptions {
2021-11-24 23:51:30 -08:00
ReplOptions { debug_asks: HashSet::new(), show_total_time: true, show_stage_times: false }
2019-05-21 02:46:07 -07:00
}
2021-10-07 01:19:35 -07:00
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 {
2021-10-13 01:41:45 -07:00
eprintln!("Error saving {} file {}", filename, err);
2021-10-07 01:19:35 -07:00
}
2019-05-14 00:40:38 -07:00
}
2021-10-13 01:41:45 -07:00
pub fn load_from_file(filename: &str) -> Result<ReplOptions, io::Error> {
2021-10-07 01:19:35 -07:00
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)
})
}
2019-05-14 00:40:38 -07:00
}