2017-06-09 13:49:21 +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-06-09 13:49:21 +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
|
|
|
|
//
|
|
|
|
|
|
|
|
//! Extension trait for libimagstore::store::Store
|
|
|
|
//!
|
|
|
|
//! This module contains traits and code for extending the Store with functions that can be used to
|
|
|
|
//! create, get and delete events.
|
|
|
|
|
|
|
|
use chrono::NaiveDateTime as NDT;
|
|
|
|
use toml::Value;
|
|
|
|
use toml_query::insert::TomlValueInsertExt;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
2019-05-17 22:14:34 +00:00
|
|
|
use failure::ResultExt;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Error;
|
2017-06-09 13:49:21 +00:00
|
|
|
|
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
|
|
|
use libimagentrydatetime::datepath::compiler::DatePathCompiler;
|
2019-08-25 09:43:17 +00:00
|
|
|
use libimagentryutil::isa::Is;
|
2017-06-09 13:49:21 +00:00
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::constants::*;
|
|
|
|
use crate::iter::get::TimeTrackingsGetIterator;
|
2019-08-25 09:43:17 +00:00
|
|
|
use crate::timetracking::IsTimeTracking;
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::tag::TimeTrackingTag as TTT;
|
2017-06-09 13:49:21 +00:00
|
|
|
|
|
|
|
pub trait TimeTrackStore<'a> {
|
|
|
|
|
|
|
|
fn create_timetracking_now(&'a self, ts: &TTT) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>;
|
|
|
|
fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>>;
|
|
|
|
|
2018-04-22 12:05:39 +00:00
|
|
|
fn get_timetrackings(&'a self) -> Result<TimeTrackingsGetIterator<'a>>;
|
2017-06-09 13:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn now() -> NDT {
|
2017-07-15 18:14:33 +00:00
|
|
|
use chrono::offset::Local;
|
2017-06-09 13:49:21 +00:00
|
|
|
Local::now().naive_local()
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref COMPILER: DatePathCompiler = {
|
|
|
|
use libimagentrydatetime::datepath::accuracy::Accuracy;
|
|
|
|
use libimagentrydatetime::datepath::format::Format;
|
|
|
|
|
|
|
|
DatePathCompiler::new(Accuracy::Second, Format::ElementIsFolder)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TimeTrackStore<'a> for Store {
|
|
|
|
|
|
|
|
fn create_timetracking_now(&'a self, ts: &TTT) -> Result<FileLockEntry<'a>> {
|
|
|
|
self.create_timetracking_at(&now(), ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_timetracking_at(&'a self, start: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>> {
|
2017-06-17 15:20:54 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2017-06-09 13:49:21 +00:00
|
|
|
COMPILER.compile(CRATE_NAME, start)
|
2019-05-17 22:14:34 +00:00
|
|
|
.context(format_err!("Failed to compile DatePath for crate '{}' with start = '{}'",
|
|
|
|
CRATE_NAME, start))
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
2017-06-17 15:20:54 +00:00
|
|
|
.map(|mut id| {
|
|
|
|
id.local_push(PathBuf::from(ts.as_str()));
|
|
|
|
id
|
|
|
|
})
|
2018-10-30 17:40:51 +00:00
|
|
|
.and_then(|id| self.create(id))
|
2017-06-09 13:49:21 +00:00
|
|
|
.and_then(|mut fle| {
|
|
|
|
let v = Value::String(ts.as_str().to_owned());
|
|
|
|
fle.get_header_mut()
|
|
|
|
.insert(DATE_TIME_TAG_HEADER_PATH, v)
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
2017-06-09 13:49:21 +00:00
|
|
|
.map(|_| fle)
|
|
|
|
})
|
|
|
|
.and_then(|mut fle| {
|
|
|
|
let v = Value::String(start.format(DATE_TIME_FORMAT).to_string());
|
|
|
|
fle.get_header_mut()
|
|
|
|
.insert(DATE_TIME_START_HEADER_PATH, v)
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
2017-06-09 13:49:21 +00:00
|
|
|
.map(|_| fle)
|
|
|
|
})
|
2019-08-25 09:43:17 +00:00
|
|
|
.and_then(|mut fle| {
|
|
|
|
fle.set_isflag::<IsTimeTracking>().map(|_| fle)
|
|
|
|
})
|
2017-06-09 13:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn create_timetracking(&'a self, start: &NDT, end: &NDT, ts: &TTT) -> Result<FileLockEntry<'a>> {
|
|
|
|
self.create_timetracking_at(start, ts)
|
|
|
|
.and_then(|mut fle| {
|
|
|
|
let v = Value::String(end.format(DATE_TIME_FORMAT).to_string());
|
|
|
|
fle.get_header_mut()
|
|
|
|
.insert(DATE_TIME_END_HEADER_PATH, v)
|
2018-10-30 17:40:51 +00:00
|
|
|
.map_err(Error::from)
|
2017-06-09 13:49:21 +00:00
|
|
|
.map(|_| fle)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-22 12:05:39 +00:00
|
|
|
fn get_timetrackings(&'a self) -> Result<TimeTrackingsGetIterator<'a>> {
|
2019-05-18 11:37:46 +00:00
|
|
|
Ok(TimeTrackingsGetIterator::new(self.entries()?.in_collection("timetrack")?, self))
|
2017-06-09 13:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|