993917855d42237c2b90f17488c4c2ef7d416bb1
[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: RGB,
21 pub hot_color: RGB,
22 // Average temperature between CPU and GPU.
23 pub cold_temperature: f32,
24 pub hot_temperature: f32,
25 }
26
27 type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
28
29 impl Settings {
30 fn default() -> Self {
31 Settings {
32 machine_name: MachineName::Jiji,
33 cold_color: RGB {
34 red: 0,
35 green: 255,
36 blue: 40,
37 },
38 hot_color: RGB {
39 red: 255,
40 green: 0,
41 blue: 0,
42 },
43 cold_temperature: 55.,
44 hot_temperature: 75.,
45 }
46 }
47
48 pub fn read(file_path: &str) -> Result<Settings> {
49 match File::open(file_path) {
50 Ok(file) => from_reader(file).map_err(|e| e.into()),
51 Err(_) => {
52 let file = File::create(file_path)?;
53 let default_config = Settings::default();
54 to_writer_pretty(file, &default_config, PrettyConfig::new())?;
55 Ok(default_config)
56 }
57 }
58 }
59 }