2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
|
|
|
use libimagrt::runtime::Runtime;
|
2018-10-30 17:40:53 +00:00
|
|
|
use libimagerror::errors::ErrorMsg as EM;
|
2017-09-14 16:58:40 +00:00
|
|
|
|
|
|
|
use toml::Value;
|
|
|
|
use toml_query::read::TomlValueReadExt;
|
2018-10-30 17:40:53 +00:00
|
|
|
use failure::Error;
|
|
|
|
use failure::Fallible as Result;
|
|
|
|
use failure::ResultExt;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
pub fn get_diary_name(rt: &Runtime) -> Option<String> {
|
|
|
|
use libimagdiary::config::get_default_diary_name;
|
|
|
|
|
2018-02-20 16:27:42 +00:00
|
|
|
rt.cli()
|
|
|
|
.value_of("diaryname")
|
|
|
|
.map(String::from)
|
|
|
|
.or_else(|| get_default_diary_name(rt))
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 16:58:40 +00:00
|
|
|
pub enum Timed {
|
2018-03-02 13:37:44 +00:00
|
|
|
Daily,
|
2017-09-14 16:58:40 +00:00
|
|
|
Hourly,
|
|
|
|
Minutely,
|
2018-02-04 18:50:05 +00:00
|
|
|
Secondly,
|
2017-09-14 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the diary should always create timed entries, which is whenever
|
|
|
|
///
|
|
|
|
/// ```toml
|
|
|
|
/// diary.diaries.<diary>.timed = true
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// * Ok(Some(Timed::Hourly)) if diary should create timed entries
|
|
|
|
/// * Ok(Some(Timed::Minutely)) if diary should not create timed entries
|
|
|
|
/// * Ok(None) if config is not available
|
|
|
|
/// * Err(e) if reading the toml failed, type error or something like this
|
|
|
|
///
|
|
|
|
pub fn get_diary_timed_config(rt: &Runtime, diary_name: &str) -> Result<Option<Timed>> {
|
|
|
|
match rt.config() {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(cfg) => {
|
|
|
|
let v = cfg
|
|
|
|
.read(&format!("diary.diaries.{}.timed", diary_name))
|
2018-10-30 17:40:53 +00:00
|
|
|
.context(EM::IO)
|
|
|
|
.map_err(Error::from);
|
2017-09-14 16:58:40 +00:00
|
|
|
|
|
|
|
match v {
|
|
|
|
Ok(Some(&Value::String(ref s))) => parse_timed_string(s, diary_name).map(Some),
|
|
|
|
|
|
|
|
Ok(Some(_)) => {
|
2018-03-02 13:37:44 +00:00
|
|
|
let s = format!("Type error at 'diary.diaryies.{}.timed': should be either 'd'/'daily', 'h'/'hourly', 'm'/'minutely' or 's'/'secondly'", diary_name);
|
2018-10-30 17:40:53 +00:00
|
|
|
Err(format_err!("{}", s))
|
2017-09-14 16:58:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Ok(None) => Ok(None),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_timed_string(s: &str, diary_name: &str) -> Result<Timed> {
|
2018-03-02 13:37:44 +00:00
|
|
|
if s == "d" || s == "daily" {
|
|
|
|
Ok(Timed::Daily)
|
|
|
|
} else if s == "h" || s == "hourly" {
|
2017-09-14 16:58:40 +00:00
|
|
|
Ok(Timed::Hourly)
|
|
|
|
} else if s == "m" || s == "minutely" {
|
|
|
|
Ok(Timed::Minutely)
|
2018-02-04 18:50:05 +00:00
|
|
|
} else if s == "s" || s == "secondly" {
|
|
|
|
Ok(Timed::Secondly)
|
2017-09-14 16:58:40 +00:00
|
|
|
} else {
|
|
|
|
let s = format!("Cannot parse config: 'diary.diaries.{}.timed = {}'",
|
|
|
|
diary_name, s);
|
2018-10-30 17:40:53 +00:00
|
|
|
Err(format_err!("{}", s))
|
2017-09-14 16:58:40 +00:00
|
|
|
}
|
|
|
|
}
|