refactor: Properly handling config serde
- Also added some trailing commas for consistence
This commit is contained in:
parent
20a5f43d7f
commit
51e7dc047a
4 changed files with 32 additions and 66 deletions
2
TODO
2
TODO
|
|
@ -3,5 +3,3 @@ feat:
|
||||||
command line interface
|
command line interface
|
||||||
proper config path handling
|
proper config path handling
|
||||||
more coloring options
|
more coloring options
|
||||||
refactor:
|
|
||||||
remove weird RawConfig struct
|
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,12 @@
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
struct RawColors {
|
#[serde(default)]
|
||||||
pub background: Option<u32>,
|
|
||||||
pub timeline: Option<u32>,
|
|
||||||
pub background_text: Option<u32>
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Colors {
|
pub struct Colors {
|
||||||
pub background: u32,
|
pub background: u32,
|
||||||
pub timeline: u32,
|
pub timeline: u32,
|
||||||
pub background_text: u32
|
pub background_text: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Colors {
|
impl Default for Colors {
|
||||||
|
|
@ -20,37 +14,21 @@ impl Default for Colors {
|
||||||
Colors {
|
Colors {
|
||||||
background: 0xff_808080,
|
background: 0xff_808080,
|
||||||
timeline: 0xff_a9a9a9,
|
timeline: 0xff_a9a9a9,
|
||||||
background_text: 0xff_000000
|
background_text: 0xff_000000,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<RawColors> for Colors {
|
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||||
fn from(value: RawColors) -> Self {
|
#[serde(default)]
|
||||||
let default_colors: Colors = Default::default();
|
|
||||||
Colors {
|
|
||||||
background: value.background.unwrap_or(default_colors.background),
|
|
||||||
timeline: value.timeline.unwrap_or(default_colors.timeline),
|
|
||||||
background_text: value.background_text.unwrap_or(default_colors.background_text),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct RawConfig {
|
|
||||||
pub log_path: Option<PathBuf>,
|
|
||||||
pub colors: Option<RawColors>,
|
|
||||||
pub event_colors: Option<Vec<u32>>,
|
|
||||||
pub text_colors: Option<Vec<u32>>
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// directory, where config is located
|
/// directory, where config is located
|
||||||
|
#[serde(skip)]
|
||||||
pub conf_path: PathBuf,
|
pub conf_path: PathBuf,
|
||||||
pub log_path: PathBuf,
|
pub log_path: PathBuf,
|
||||||
pub colors: Colors,
|
pub colors: Colors,
|
||||||
pub event_colors: Vec<u32>,
|
pub event_colors: Vec<u32>,
|
||||||
pub text_colors: Vec<u32>
|
pub text_colors: Vec<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
|
@ -98,24 +76,7 @@ impl Default for Config {
|
||||||
log_path: PathBuf::from("./logs"),
|
log_path: PathBuf::from("./logs"),
|
||||||
colors,
|
colors,
|
||||||
event_colors,
|
event_colors,
|
||||||
text_colors
|
text_colors,
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RawConfig> for Config {
|
|
||||||
fn from(value: RawConfig) -> Self {
|
|
||||||
let default_config: Config = Default::default();
|
|
||||||
let colors: Colors = match value.colors {
|
|
||||||
Some(raw_colors) => raw_colors.into(),
|
|
||||||
None => default_config.colors.clone()
|
|
||||||
};
|
|
||||||
Config {
|
|
||||||
conf_path: default_config.conf_path,
|
|
||||||
log_path: value.log_path.unwrap_or(default_config.log_path),
|
|
||||||
colors,
|
|
||||||
event_colors: value.event_colors.unwrap_or(default_config.event_colors.clone()),
|
|
||||||
text_colors: value.text_colors.unwrap_or(default_config.text_colors.clone())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -131,9 +92,7 @@ impl Config {
|
||||||
|
|
||||||
pub fn load(path: PathBuf) -> Self {
|
pub fn load(path: PathBuf) -> Self {
|
||||||
if let Ok(toml_string) = std::fs::read_to_string(path.clone()) {
|
if let Ok(toml_string) = std::fs::read_to_string(path.clone()) {
|
||||||
let conf = toml::from_str::<RawConfig>(&toml_string);
|
if let Ok(mut conf) = toml::from_str::<Config>(&toml_string) {
|
||||||
if let Ok(raw_conf) = conf {
|
|
||||||
let mut conf: Config = raw_conf.into();
|
|
||||||
conf.conf_path = path.parent().unwrap().into();
|
conf.conf_path = path.parent().unwrap().into();
|
||||||
return conf;
|
return conf;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,10 @@ use serde::{Deserialize, Serialize};
|
||||||
use toml::value::{Date, Time};
|
use toml::value::{Date, Time};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Log {
|
pub struct Log {
|
||||||
pub date: Date,
|
pub date: Date,
|
||||||
pub events: Vec<Event>
|
pub events: Vec<Event>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Log {
|
impl Log {
|
||||||
|
|
@ -64,12 +64,12 @@ impl Log {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
|
||||||
pub struct Event {
|
pub struct Event {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub start: Time,
|
pub start: Time,
|
||||||
pub end: Time,
|
pub end: Time,
|
||||||
pub finished: bool
|
pub finished: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
|
|
|
||||||
19
src/main.rs
19
src/main.rs
|
|
@ -238,15 +238,24 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let ui = ui_weak.unwrap();
|
let ui = ui_weak.unwrap();
|
||||||
let prev_event_name = {
|
let prev_event_name = {
|
||||||
let log_guard = log.lock().expect("Log shouldn't be used twice");
|
let log_guard = log.lock().expect("Log shouldn't be used twice");
|
||||||
let prev_index = log_guard.events.len() - 2;
|
match log_guard.events.len().checked_sub(2) {
|
||||||
log_guard.events
|
Some(prev_index) => {
|
||||||
|
let prev_event = log_guard.events
|
||||||
.get(prev_index)
|
.get(prev_index)
|
||||||
.expect("Index is already checked")
|
.expect("Index is already checked")
|
||||||
.name
|
.name
|
||||||
.clone()
|
.clone();
|
||||||
|
Some(prev_event)
|
||||||
|
},
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
};
|
};
|
||||||
println!("{}", prev_event_name.as_str());
|
if let Some(name) = prev_event_name {
|
||||||
ui.set_previous_event_name(prev_event_name.to_shared_string());
|
println!("{name}");
|
||||||
|
ui.set_previous_event_name(name.to_shared_string());
|
||||||
|
} else {
|
||||||
|
ui.set_previous_event_name(SharedString::new());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue