Add a color (2 colors can be now defined for a machine).
[temp2RGB.git] / src / settings.rs
1 use std::fs::File;
2
3 use ron::{
4 de::from_reader,
5 ser::{to_writer_pretty, PrettyConfig},
6 };
7 use serde::{Deserialize, Serialize};
8
9 use crate::rgb::RGB;
10
11 #[derive(Debug, Deserialize, Serialize)]
12 pub enum MachineName {
13 Jiji,
14 LyssMetal,
15 }
16
17 #[derive(Debug, Deserialize, Serialize)]
18 pub struct Settings {
19 pub machine_name: MachineName,
20 pub cold_color_1: RGB,
21 pub hot_color_1: RGB,
22 pub cold_color_2: Option<RGB>,
23 pub hot_color_2: Option<RGB>,
24 // Average temperature between CPU and GPU.
25 pub cold_temperature: f32,
26 pub hot_temperature: f32,
27 }
28
29 type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
30
31 impl Settings {
32 fn default() -> Self {
33 Settings {
34 machine_name: MachineName::Jiji,
35 cold_color_1: RGB {
36 red: 0,
37 green: 255,
38 blue: 40,
39 },
40 hot_color_1: RGB {
41 red: 255,
42 green: 0,
43 blue: 0,
44 },
45 cold_color_2: None,
46 hot_color_2: None,
47 cold_temperature: 55.,
48 hot_temperature: 75.,
49 }
50 }
51
52 pub fn read(file_path: &str) -> Result<Settings> {
53 match File::open(file_path) {
54 Ok(file) => from_reader(file).map_err(|e| e.into()),
55 Err(_) => {
56 let file = File::create(file_path)?;
57 let default_config = Settings::default();
58 to_writer_pretty(file, &default_config, PrettyConfig::new())?;
59 Ok(default_config)
60 }
61 }
62 }
63 }