Skip to content

Commit

Permalink
Serialize/deserialize ThemeSetting directly (leftwm#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
p3s authored Oct 26, 2020
1 parent ee5944d commit 644e0bc
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions src/config/theme_setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,27 @@ use std::default::Default;
use std::fs;
use std::path::PathBuf;

#[derive(Serialize, Deserialize, Debug, Clone)]
struct ThemeSettingLoadable {
border_width: Option<u32>,
margin: Option<u32>,
default_border_color: Option<String>,
floating_border_color: Option<String>,
focused_border_color: Option<String>,
on_new_window: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(default)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ThemeSetting {
pub border_width: u32,
pub margin: u32,
pub default_border_color: String,
pub floating_border_color: String,
pub focused_border_color: String,
#[serde(rename = "on_new_window")]
pub on_new_window_cmd: Option<String>,
}

/// Convert Theme Setting Loadable into Theme Settings
/// This will only override fields that have been provided
impl From<ThemeSettingLoadable> for ThemeSetting {
fn from(file: ThemeSettingLoadable) -> Self {
let mut theme = ThemeSetting::default();
if let Some(x) = file.border_width {
theme.border_width = x
}
if let Some(x) = file.margin {
theme.margin = x
}
if let Some(x) = file.default_border_color {
theme.default_border_color = x
}
if let Some(x) = file.floating_border_color {
theme.floating_border_color = x
}
if let Some(x) = file.focused_border_color {
theme.focused_border_color = x
}
theme.on_new_window_cmd = file.on_new_window;
theme
}
}

impl ThemeSetting {
pub fn load(path: &PathBuf) -> ThemeSetting {
let mut theme = ThemeSetting::default();
if let Ok(file) = load_theme_file(path) {
theme = file.into();
}
theme
load_theme_file(path).unwrap_or_default()
}
}

fn load_theme_file(path: &PathBuf) -> Result<ThemeSettingLoadable> {
fn load_theme_file(path: &PathBuf) -> Result<ThemeSetting> {
let contents = fs::read_to_string(path)?;
let from_file: ThemeSettingLoadable = toml::from_str(&contents)?;
let from_file: ThemeSetting = toml::from_str(&contents)?;
Ok(from_file)
}

Expand All @@ -77,3 +40,42 @@ impl Default for ThemeSetting {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn deserialize_empty_theme_config() {
let config = "";
let config: ThemeSetting = toml::from_str(config).unwrap();
let default_config = ThemeSetting::default();

assert_eq!(config, default_config);
}

#[test]
fn deserialize_custom_theme_config() {
let config = r#"
border_width = 0
margin = 5
default_border_color = '#222222'
floating_border_color = '#005500'
focused_border_color = '#FFB53A'
on_new_window = 'echo Hello World'
"#;
let config: ThemeSetting = toml::from_str(config).unwrap();

assert_eq!(
config,
ThemeSetting {
border_width: 0,
margin: 5,
default_border_color: "#222222".to_string(),
floating_border_color: "#005500".to_string(),
focused_border_color: "#FFB53A".to_string(),
on_new_window_cmd: Some("echo Hello World".to_string()),
}
);
}
}

0 comments on commit 644e0bc

Please sign in to comment.