Made every thing in Config optional internally
This commit is contained in:
parent
1b6f8ef282
commit
ba976d9e12
1 changed files with 70 additions and 12 deletions
|
@ -2,16 +2,50 @@ use std::path::PathBuf;
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct RawColors {
|
||||
pub background: Option<u32>,
|
||||
pub timeline: Option<u32>,
|
||||
pub background_text: Option<u32>
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Colors {
|
||||
pub background: u32,
|
||||
pub timeline: u32,
|
||||
pub background_text: u32
|
||||
}
|
||||
|
||||
impl Default for Colors {
|
||||
fn default() -> Self {
|
||||
Colors {
|
||||
background: 0xff_808080,
|
||||
timeline: 0xff_a9a9a9,
|
||||
background_text: 0xff_000000
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RawColors> for Colors {
|
||||
fn from(value: RawColors) -> Self {
|
||||
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 {
|
||||
/// directory, where config is located
|
||||
#[serde(skip)]
|
||||
pub conf_path: PathBuf,
|
||||
pub log_path: PathBuf,
|
||||
pub colors: Colors,
|
||||
|
@ -19,14 +53,10 @@ pub struct Config {
|
|||
pub text_colors: Vec<u32>
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(conf_path: PathBuf) -> Self {
|
||||
let conf_dir: PathBuf = conf_path.parent().unwrap().into();
|
||||
let colors = Colors {
|
||||
background: 0xff_808080,
|
||||
timeline: 0xff_a9a9a9,
|
||||
background_text: 0xff_000000
|
||||
};
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
let conf_path = PathBuf::new();
|
||||
let colors: Colors = Default::default();
|
||||
let event_colors: Vec<u32> = vec![
|
||||
0xff_97f9f9,
|
||||
0xff_a4def9,
|
||||
|
@ -64,18 +94,46 @@ impl Config {
|
|||
0xff000000
|
||||
];
|
||||
Config {
|
||||
conf_path: conf_dir,
|
||||
conf_path,
|
||||
log_path: PathBuf::from("./logs"),
|
||||
colors,
|
||||
event_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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(conf_path: PathBuf) -> Self {
|
||||
let conf_dir: PathBuf = conf_path.parent().unwrap().into();
|
||||
Config {
|
||||
conf_path: conf_dir,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load(path: PathBuf) -> Self {
|
||||
if let Ok(toml_string) = std::fs::read_to_string(path.clone()) {
|
||||
let conf = toml::from_str::<Self>(&toml_string);
|
||||
if let Ok(mut conf) = conf {
|
||||
let conf = toml::from_str::<RawConfig>(&toml_string);
|
||||
if let Ok(raw_conf) = conf {
|
||||
let mut conf: Config = raw_conf.into();
|
||||
conf.conf_path = path.parent().unwrap().into();
|
||||
return conf;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue