summaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 083f5d2f23471203f68c7f51db17cd589c7ccbe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::collections::HashMap;
use toml;
use yansi::{self,Style,Color};

use crate::defaults;
use crate::utils::fileutil as utils;

#[derive(Deserialize,Debug,PartialEq)]
#[serde(default)]
pub struct Config {
  pub calendars: HashMap<String,CalendarConfig>,
  pub agenda: AgendaConfig,
  pub local_tz: Option<LocalTZConfig>
}

#[derive(Deserialize,Debug,PartialEq)]
#[serde(default)]
pub struct AgendaConfig {
  pub print_week_separator: bool,
  pub print_empty_days: bool,
}

#[derive(Deserialize,Debug,PartialEq)]
pub struct CalendarConfig {
  pub color: Option<u8>
}

#[derive(Deserialize,Debug,PartialEq)]
pub struct LocalTZConfig {
  pub timezone: String
}

impl Config {
  pub fn get_config_for_calendar(&self,  calendar_name: &str) -> Option<&CalendarConfig> {
    self.calendars.get(calendar_name)
  }

  pub fn read_config() -> Self {
    let config = utils::read_file_to_string(&defaults::get_configfile());
    match config {
      Ok(config) => toml::from_str(&config).unwrap(),
      Err(_) => Config::default()
    }
  }
}

impl CalendarConfig {
  pub fn get_style_for_calendar(&self) -> yansi::Style {
    let mut style = Style::default();
    if let Some(color) = self.color {
      style = style.fg(Color::Fixed(color));
    }
    style
  }
}

impl LocalTZConfig {
  pub fn get_local_tz(&self) -> String {
    self.timezone.clone()
  }
}

impl Default for AgendaConfig {
  fn default() -> Self {
    AgendaConfig {
      print_week_separator: false,
      print_empty_days: true,
    }
  }
}

impl Default for Config {
  fn default() -> Self {
    Config {
      agenda: AgendaConfig::default(),
      calendars: HashMap::new(),
      local_tz: None,
    }
  }
}

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

  #[test]
  fn test_read_config_none() {
    let _testdir = testutils::prepare_testdir("testdir");

    let config = Config::read_config();

    assert_eq!(Config::default(), config);
  }

  #[test]
  fn test_read_config() {
    let _testdir = testutils::prepare_testdir("testdir_config");

    let config = Config::read_config();
    let cal_config = config.get_config_for_calendar("sample").unwrap();

    let expected = Config {
      calendars: hashmap!{"sample".to_string() => CalendarConfig { color: Some(81) }},
      agenda: AgendaConfig {
        print_week_separator: true,
        print_empty_days: false
      },
      local_tz: None,
    };

    assert_eq!(expected, config);
    assert_eq!(expected.calendars.get("sample").unwrap(), cal_config);
  }

  #[test]
  fn test_get_style_for_calendar() {
    let config = CalendarConfig { color: Some(81) };
    let style = config.get_style_for_calendar();

    assert_eq!(Color::Fixed(81).style(), style);
  }

  #[test]
  fn test_get_local_tz() {
    let config = LocalTZConfig { timezone: "Europe/Berlin".to_string() };
    let tz = config.get_local_tz();

    assert_eq!("Europe/Berlin".to_string(), tz);
  }
}