use crate::data::kusa_color::KusaColor;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::fs::OpenOptions;
use std::io::Read;
use std::io::Write;
use std::path::Path;
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")] pub struct Settings {
pub image_file: String,
pub image_width: u32,
pub image_height: u32,
pub canvas_zoom: f64,
pub canvas_margin_top: f64,
pub canvas_margin_right: f64,
pub canvas_margin_bottom: f64,
pub canvas_margin_left: f64,
pub canvas_cell_size: f64,
pub canvas_grid_thickness: f64,
pub canvas_grid_color: [f32; 4],
pub paint_tool: String,
pub paint_nib: String,
pub paint_color: KusaColor,
pub paint_thickness: f64,
}
impl Default for Settings {
fn default() -> Self {
Settings {
image_file: "./work/image.png".to_string(),
image_width: 64,
image_height: 32,
canvas_zoom: 1.0,
canvas_margin_top: 8.0,
canvas_margin_right: 8.0,
canvas_margin_bottom: 8.0,
canvas_margin_left: 8.0,
canvas_cell_size: 8.0,
canvas_grid_thickness: 0.5,
canvas_grid_color: [0.0, 0.0, 0.0, 1.0],
paint_tool: "Pen".to_string(),
paint_nib: "Circle".to_string(),
paint_color: KusaColor {
r: 230,
g: 230,
b: 180,
a: 255,
},
paint_thickness: 1.0,
}
}
}
impl Settings {
pub fn load(settings_path: &str) -> Result<Self, String> {
let path = Path::new(settings_path);
let mut file = match File::open(path) {
Ok(x) => x,
Err(why) => return Err(format!("{}", why)),
};
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(x) => x,
Err(why) => return Err(format!("{}", why)),
};
match serde_json::from_str(&contents) {
Ok(x) => return Ok(x),
Err(why) => return Err(format!("Settings load fail. {}", why)),
}
}
pub fn save(&self, settings_path: &str) {
let path = Path::new(settings_path);
let mut file = match OpenOptions::new().write(true).create(true).open(path) {
Ok(file) => file,
Err(err) => panic!("Log file open error. {:?}", err),
};
let json_str = match serde_json::to_string(self) {
Ok(x) => x,
Err(why) => panic!("couldn't serialize settings to json. : {}", &why),
};
if let Err(why) = file.write_all(json_str.as_bytes()) {
panic!("couldn't write log. : {}", &why)
}
}
}