summaryrefslogtreecommitdiff
path: root/lib/entry/libimagentrytimetrack/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/entry/libimagentrytimetrack/src')
-rw-r--r--lib/entry/libimagentrytimetrack/src/constants.rs25
-rw-r--r--lib/entry/libimagentrytimetrack/src/error.rs39
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/create.rs77
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/filter.rs124
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/get.rs65
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/mod.rs63
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/setendtime.rs65
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/storeid.rs73
-rw-r--r--lib/entry/libimagentrytimetrack/src/iter/tag.rs55
-rw-r--r--lib/entry/libimagentrytimetrack/src/lib.rs45
-rw-r--r--lib/entry/libimagentrytimetrack/src/result.rs26
-rw-r--r--lib/entry/libimagentrytimetrack/src/tag.rs69
-rw-r--r--lib/entry/libimagentrytimetrack/src/timetracking.rs152
-rw-r--r--lib/entry/libimagentrytimetrack/src/timetrackingstore.rs114
14 files changed, 992 insertions, 0 deletions
diff --git a/lib/entry/libimagentrytimetrack/src/constants.rs b/lib/entry/libimagentrytimetrack/src/constants.rs
new file mode 100644
index 0000000..63eeaf6
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/constants.rs
@@ -0,0 +1,25 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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
+//
+
+pub const CRATE_NAME : &'static str = "timetrack";
+pub const DATE_TIME_FORMAT : &'static str = "%Y-%m-%dT%H:%M:%S";
+pub const DATE_TIME_START_HEADER_PATH : &'static str = "timetrack.start";
+pub const DATE_TIME_END_HEADER_PATH : &'static str = "timetrack.end";
+pub const DATE_TIME_TAG_HEADER_PATH : &'static str = "timetrack.tag";
+
diff --git a/lib/entry/libimagentrytimetrack/src/error.rs b/lib/entry/libimagentrytimetrack/src/error.rs
new file mode 100644
index 0000000..3ac4a11
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/error.rs
@@ -0,0 +1,39 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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
+//
+
+generate_error_module!(
+ generate_error_types!(TimeTrackError, TimeTrackErrorKind,
+ StoreReadError => "Store read error",
+ StoreWriteError => "Store write error",
+
+ StoreIdError => "Error while handling StoreId",
+
+ TagFormat => "Tag has invalid format",
+
+ HeaderReadError => "Error writing header",
+ HeaderWriteError => "Error writing header",
+ HeaderFieldTypeError => "Type error in header",
+ DateTimeParserError => "Error while parsing DateTime"
+ );
+);
+
+pub use self::error::TimeTrackError;
+pub use self::error::TimeTrackErrorKind;
+pub use self::error::MapErrInto;
+
diff --git a/lib/entry/libimagentrytimetrack/src/iter/create.rs b/lib/entry/libimagentrytimetrack/src/iter/create.rs
new file mode 100644
index 0000000..0c471aa
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/create.rs
@@ -0,0 +1,77 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 toml::Value;
+use toml_query::insert::TomlValueInsertExt;
+use chrono::naive::NaiveDateTime as NDT;
+
+use constants::*;
+use error::TimeTrackError as TTE;
+use error::TimeTrackErrorKind as TTEK;
+use error::MapErrInto;
+use iter::storeid::TagStoreIdIter;
+use iter::setendtime::SetEndTimeIter;
+
+use libimagstore::store::FileLockEntry;
+use libimagstore::store::Store;
+
+pub struct CreateTimeTrackIter<'a> {
+ inner: TagStoreIdIter,
+ store: &'a Store,
+}
+
+impl<'a> CreateTimeTrackIter<'a>
+{
+ pub fn new(inner: TagStoreIdIter, store: &'a Store) -> CreateTimeTrackIter<'a> {
+ CreateTimeTrackIter {
+ inner: inner,
+ store: store,
+ }
+ }
+
+ pub fn set_end_time(self, datetime: NDT) -> SetEndTimeIter<'a> {
+ SetEndTimeIter::new(self, datetime)
+ }
+}
+
+impl<'a> Iterator for CreateTimeTrackIter<'a>
+{
+ type Item = Result<FileLockEntry<'a>, TTE>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.inner
+ .next()
+ .map(|res| {
+ res.and_then(|(id, starttime)| {
+ self.store
+ .create(id)
+ .map_err_into(TTEK::StoreWriteError)
+ .and_then(|mut entry| {
+ let v = Value::String(starttime.format(DATE_TIME_FORMAT).to_string());
+ entry.get_header_mut()
+ .insert(DATE_TIME_START_HEADER_PATH, v)
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| entry)
+ })
+ })
+ })
+ }
+
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/iter/filter.rs b/lib/entry/libimagentrytimetrack/src/iter/filter.rs
new file mode 100644
index 0000000..0a3ae53
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/filter.rs
@@ -0,0 +1,124 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 result::Result;
+
+use chrono::NaiveDateTime;
+use filters::filter::Filter;
+
+use libimagstore::store::FileLockEntry;
+
+use tag::TimeTrackingTag as TTT;
+use timetracking::TimeTracking;
+
+
+pub fn has_start_time(entry: &FileLockEntry) -> bool {
+ is_match!(entry.get_start_datetime(), Ok(Some(_)))
+}
+
+pub fn has_end_time(entry: &FileLockEntry) -> bool {
+ is_match!(entry.get_end_datetime(), Ok(Some(_)))
+}
+
+pub fn has_tag(entry: &FileLockEntry) -> bool {
+ is_match!(entry.get_timetrack_tag(), Ok(_))
+}
+
+pub fn has_start_time_where<F>(f: F) -> HasStartTimeWhere<F>
+ where F: Fn(&NaiveDateTime) -> bool
+{
+ HasStartTimeWhere::new(f)
+}
+
+pub fn has_end_time_where<F>(f: F) -> HasEndTimeWhere<F>
+ where F: Fn(&NaiveDateTime) -> bool
+{
+ HasEndTimeWhere::new(f)
+}
+
+pub fn has_one_of_tags<'a>(tags: &'a Vec<TTT>) -> HasOneOfTags<'a> {
+ HasOneOfTags::new(tags)
+}
+
+mod types {
+ use chrono::NaiveDateTime;
+ use filters::filter::Filter;
+
+ use tag::TimeTrackingTag as TTT;
+ use timetracking::TimeTracking;
+
+ use libimagstore::store::FileLockEntry;
+
+ pub struct HasStartTimeWhere<F>(F)
+ where F: Fn(&NaiveDateTime) -> bool;
+
+ impl<F: Fn(&NaiveDateTime) -> bool> HasStartTimeWhere<F> {
+ pub fn new(f: F) -> HasStartTimeWhere<F> {
+ HasStartTimeWhere(f)
+ }
+ }
+
+ impl<'a, F> Filter<FileLockEntry<'a>> for HasStartTimeWhere<F>
+ where F: Fn(&NaiveDateTime) -> bool
+ {
+ fn filter(&self, entry: &FileLockEntry) -> bool {
+ entry.get_start_datetime()
+ .map(|o| o.map(|dt| (self.0)(&dt)).unwrap_or(false))
+ .unwrap_or(false)
+ }
+ }
+
+ pub struct HasEndTimeWhere<F>(F)
+ where F: Fn(&NaiveDateTime) -> bool;
+
+ impl<F: Fn(&NaiveDateTime) -> bool> HasEndTimeWhere<F> {
+ pub fn new(f: F) -> HasEndTimeWhere<F> {
+ HasEndTimeWhere(f)
+ }
+ }
+
+ impl<'a, F> Filter<FileLockEntry<'a>> for HasEndTimeWhere<F>
+ where F: Fn(&NaiveDateTime) -> bool
+ {
+ fn filter(&self, entry: &FileLockEntry) -> bool {
+ entry.get_end_datetime()
+ .map(|o| o.map(|dt| (self.0)(&dt)).unwrap_or(false))
+ .unwrap_or(false)
+ }
+ }
+
+ pub struct HasOneOfTags<'a>(&'a Vec<TTT>);
+
+ impl<'a> HasOneOfTags<'a> {
+ pub fn new(tags: &'a Vec<TTT>) -> HasOneOfTags<'a> {
+ HasOneOfTags(tags)
+ }
+ }
+
+ impl<'a, 'b> Filter<FileLockEntry<'b>> for HasOneOfTags<'a> {
+ fn filter(&self, entry: &FileLockEntry) -> bool {
+ entry.get_timetrack_tag().map(|t| self.0.contains(&t)).unwrap_or(false)
+ }
+ }
+
+}
+pub use self::types::HasStartTimeWhere;
+pub use self::types::HasEndTimeWhere;
+pub use self::types::HasOneOfTags;
+
diff --git a/lib/entry/libimagentrytimetrack/src/iter/get.rs b/lib/entry/libimagentrytimetrack/src/iter/get.rs
new file mode 100644
index 0000000..62e0a85
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/get.rs
@@ -0,0 +1,65 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 error::TimeTrackError as TTE;
+use error::TimeTrackErrorKind as TTEK;
+use error::MapErrInto;
+
+use libimagstore::store::FileLockEntry;
+use libimagstore::store::Store;
+use libimagstore::storeid::StoreIdIterator;
+use libimagerror::into::IntoError;
+
+pub struct GetTimeTrackIter<'a>{
+ inner: StoreIdIterator,
+ store: &'a Store,
+}
+
+impl<'a> GetTimeTrackIter<'a> {
+
+ pub fn new(sidit: StoreIdIterator, store: &'a Store) -> GetTimeTrackIter<'a> {
+ GetTimeTrackIter {
+ inner: sidit,
+ store: store
+ }
+ }
+}
+
+impl<'a> Iterator for GetTimeTrackIter<'a> {
+ type Item = Result<FileLockEntry<'a>, TTE>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.inner.next().map(|sid| {
+ match self.store.get(sid).map_err_into(TTEK::StoreReadError) {
+ Ok(None) => Err(TTEK::StoreReadError.into_error()),
+ Ok(Some(s)) => Ok(s),
+ Err(e) => Err(e)
+ }
+ })
+ }
+}
+
+// impl<'a, I> From<I> for GetTimeTrackIter<'a, I>
+// where I: Iterator<Item = Result<FileLockEntry<'a>, TTE>>
+// {
+// fn from(i: I) -> GetTimeTrackIter<'a, I> {
+// GetTimeTrackIter(i)
+// }
+// }
+//
diff --git a/lib/entry/libimagentrytimetrack/src/iter/mod.rs b/lib/entry/libimagentrytimetrack/src/iter/mod.rs
new file mode 100644
index 0000000..467199d
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/mod.rs
@@ -0,0 +1,63 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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
+//
+
+pub mod create;
+pub mod filter;
+pub mod get;
+pub mod setendtime;
+pub mod storeid;
+pub mod tag;
+
+#[cfg(test)]
+mod test {
+ use chrono::naive::NaiveDate;
+
+ use libimagstore::store::Store;
+
+ use super::create::*;
+ use super::get::*;
+ use super::setendtime::*;
+ use super::storeid::*;
+ use super::tag::*;
+
+ fn get_store() -> Store {
+ use std::path::PathBuf;
+ use libimagstore::file_abstraction::InMemoryFileAbstraction;
+
+ let backend = Box::new(InMemoryFileAbstraction::new());
+ Store::new_with_backend(PathBuf::from("/"), None, backend).unwrap()
+ }
+
+ #[test]
+ fn test_building_chain() {
+ let now = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 1);
+ let then = NaiveDate::from_ymd(2000, 1, 1).and_hms(0, 0, 2);
+ let store = get_store();
+ let tags = vec!["foo", "bar"];
+
+ let iter = tags.into_iter().map(String::from);
+
+ let iter : SetEndTimeIter = TagIter::new(Box::new(iter))
+ .create_storeids(now)
+ .create_entries(&store)
+ .set_end_time(then);
+ // just to see whether this compiles, actually.
+ }
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/iter/setendtime.rs b/lib/entry/libimagentrytimetrack/src/iter/setendtime.rs
new file mode 100644
index 0000000..89eb382
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/setendtime.rs
@@ -0,0 +1,65 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 toml::Value;
+use toml_query::insert::TomlValueInsertExt;
+use chrono::naive::NaiveDateTime as NDT;
+
+use constants::*;
+use error::TimeTrackError as TTE;
+use error::TimeTrackErrorKind as TTEK;
+use error::MapErrInto;
+use iter::create::CreateTimeTrackIter;
+
+use libimagstore::store::FileLockEntry;
+
+pub struct SetEndTimeIter<'a> {
+ inner: CreateTimeTrackIter<'a>,
+ datetime: NDT,
+}
+
+impl<'a> SetEndTimeIter<'a>
+{
+ pub fn new(inner: CreateTimeTrackIter<'a>, datetime: NDT) -> SetEndTimeIter<'a> {
+ SetEndTimeIter {
+ inner: inner,
+ datetime: datetime,
+ }
+ }
+}
+
+impl<'a> Iterator for SetEndTimeIter<'a> {
+ type Item = Result<FileLockEntry<'a>, TTE>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.inner
+ .next()
+ .map(|res| {
+ res.and_then(|mut fle| {
+ let v = Value::String(self.datetime.format(DATE_TIME_FORMAT).to_string());
+ fle.get_header_mut()
+ .insert(DATE_TIME_END_HEADER_PATH, v)
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| fle)
+ })
+ })
+ }
+
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/iter/storeid.rs b/lib/entry/libimagentrytimetrack/src/iter/storeid.rs
new file mode 100644
index 0000000..a307190
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/storeid.rs
@@ -0,0 +1,73 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 chrono::naive::NaiveDateTime as NDT;
+
+use constants::*;
+use error::TimeTrackError;
+use error::TimeTrackErrorKind as TTEK;
+use error::MapErrInto;
+use iter::tag::TagIter;
+use iter::create::CreateTimeTrackIter;
+
+use libimagstore::store::Store;
+use libimagstore::storeid::StoreId;
+
+pub struct TagStoreIdIter {
+ inner: TagIter,
+ datetime: NDT,
+}
+
+impl TagStoreIdIter {
+
+ pub fn new(inner: TagIter, datetime: NDT) -> TagStoreIdIter {
+ TagStoreIdIter {
+ inner: inner,
+ datetime: datetime,
+ }
+ }
+
+ pub fn create_entries<'a>(self, store: &'a Store) -> CreateTimeTrackIter<'a> {
+ CreateTimeTrackIter::new(self, store)
+ }
+
+}
+
+impl Iterator for TagStoreIdIter {
+ type Item = Result<(StoreId, NDT), TimeTrackError>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ use module_path::ModuleEntryPath;
+ use libimagstore::storeid::IntoStoreId;
+
+ self.inner
+ .next()
+ .map(|res| {
+ res.and_then(|tag| {
+ let dt = self.datetime.format(DATE_TIME_FORMAT).to_string();
+ let id_str = format!("{}-{}", dt, tag.as_str());
+ ModuleEntryPath::new(id_str)
+ .into_storeid()
+ .map_err_into(TTEK::StoreIdError)
+ .map(|id| (id, self.datetime.clone()))
+ })
+ })
+ }
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/iter/tag.rs b/lib/entry/libimagentrytimetrack/src/iter/tag.rs
new file mode 100644
index 0000000..da006d8
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/iter/tag.rs
@@ -0,0 +1,55 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 chrono::naive::NaiveDateTime as NDT;
+
+use error::TimeTrackError;
+use error::TimeTrackErrorKind as TTEK;
+use tag::TimeTrackingTag as TTT;
+use iter::storeid::TagStoreIdIter;
+
+use libimagentrytag::tag::is_tag_str;
+use libimagerror::into::IntoError;
+
+pub struct TagIter(Box<Iterator<Item = String>>);
+
+impl TagIter {
+ pub fn new(i: Box<Iterator<Item = String>>) -> TagIter {
+ TagIter(i)
+ }
+
+ pub fn create_storeids(self, datetime: NDT) -> TagStoreIdIter {
+ TagStoreIdIter::new(self, datetime)
+ }
+}
+
+impl Iterator for TagIter {
+ type Item = Result<TTT, TimeTrackError>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.0
+ .next()
+ .map(|t| if is_tag_str(&t).is_ok() {
+ Ok(TTT::from(t))
+ } else {
+ Err(TTEK::TagFormat.into_error())
+ })
+ }
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/lib.rs b/lib/entry/libimagentrytimetrack/src/lib.rs
new file mode 100644
index 0000000..e3f9d2a
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/lib.rs
@@ -0,0 +1,45 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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
+//
+
+extern crate filters;
+extern crate chrono;
+extern crate toml;
+extern crate toml_query;
+#[macro_use]
+extern crate lazy_static;
+#[macro_use]
+extern crate is_match;
+
+#[macro_use]
+extern crate libimagerror;
+#[macro_use]
+extern crate libimagstore;
+extern crate libimagentrydatetime;
+extern crate libimagentrytag;
+
+mod constants;
+pub mod error;
+pub mod timetracking;
+pub mod timetrackingstore;
+pub mod iter;
+pub mod result;
+pub mod tag;
+
+module_entry_path_mod!("timetrack");
+
diff --git a/lib/entry/libimagentrytimetrack/src/result.rs b/lib/entry/libimagentrytimetrack/src/result.rs
new file mode 100644
index 0000000..8ea8173
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/result.rs
@@ -0,0 +1,26 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 std::result::Result as RResult;
+
+use error::TimeTrackError;
+
+pub type Result<T> = RResult<T, TimeTrackError>;
+
+
diff --git a/lib/entry/libimagentrytimetrack/src/tag.rs b/lib/entry/libimagentrytimetrack/src/tag.rs
new file mode 100644
index 0000000..df2f979
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/tag.rs
@@ -0,0 +1,69 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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 std::fmt::Display;
+use std::fmt::Error as FmtError;
+use std::fmt::Formatter;
+use std::path::PathBuf;
+
+use libimagstore::store::Result as StoreResult;
+use libimagstore::storeid::IntoStoreId;
+use libimagstore::storeid::StoreId;
+
+/// A tag for time-tracking. This is not a normal `libimagentrytag` tag, because we want the user
+/// give the possibility to use the tagging functionality without interfering with this functionality.
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct TimeTrackingTag(String);
+
+impl TimeTrackingTag {
+ pub fn as_str(&self) -> &str {
+ &self.0
+ }
+}
+
+impl Display for TimeTrackingTag {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
+ self.0.fmt(f)
+ }
+}
+
+impl Into<String> for TimeTrackingTag {
+ fn into(self) -> String {
+ self.0
+ }
+}
+
+impl From<String> for TimeTrackingTag {
+ fn from(s: String) -> TimeTrackingTag {
+ TimeTrackingTag(s)
+ }
+}
+
+impl<'a> From<&'a String> for TimeTrackingTag {
+ fn from(s: &'a String) -> TimeTrackingTag {
+ TimeTrackingTag(s.clone())
+ }
+}
+
+impl IntoStoreId for TimeTrackingTag {
+ fn into_storeid(self) -> StoreResult<StoreId> {
+ StoreId::new_baseless(PathBuf::from(self.0))
+ }
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/timetracking.rs b/lib/entry/libimagentrytimetrack/src/timetracking.rs
new file mode 100644
index 0000000..ba40ac6
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/timetracking.rs
@@ -0,0 +1,152 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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
+//
+
+//! Event types
+//!
+//! This module contains types that represent events. These types (which wrap FileLockEntries from
+//! the Store) represent Events, thus they have functionality for settings the start and end-time,
+//! getting the start and end time and also deleting start and end time.
+//!
+
+use chrono::naive::NaiveDateTime;
+
+use libimagstore::store::Entry;
+use libimagerror::into::IntoError;
+
+use tag::TimeTrackingTag as TTT;
+use error::TimeTrackErrorKind as TTEK;
+use error::MapErrInto;
+use result::Result;
+use constants::*;
+
+use toml::Value;
+use toml_query::delete::TomlValueDeleteExt;
+use toml_query::insert::TomlValueInsertExt;
+use toml_query::read::TomlValueReadExt;
+
+pub trait TimeTracking {
+
+ fn get_timetrack_tag(&self) -> Result<TTT>;
+
+ fn set_start_datetime(&mut self, dt: NaiveDateTime) -> Result<()>;
+
+ fn get_start_datetime(&self) -> Result<Option<NaiveDateTime>>;
+
+ fn delete_start_datetime(&mut self) -> Result<()>;
+
+ fn set_end_datetime(&mut self, dt: NaiveDateTime) -> Result<()>;
+
+ fn get_end_datetime(&self) -> Result<Option<NaiveDateTime>>;
+
+ fn delete_end_datetime(&mut self) -> Result<()>;
+
+ fn valid(&self) -> Result<bool>;
+
+}
+
+impl TimeTracking for Entry {
+
+ fn get_timetrack_tag(&self) -> Result<TTT> {
+ self.get_header()
+ .read(DATE_TIME_TAG_HEADER_PATH)
+ .map_err_into(TTEK::HeaderReadError)
+ .and_then(|value| match value {
+ Some(&Value::String(ref s)) => Ok(s.clone().into()),
+ Some(_) => Err(TTEK::HeaderFieldTypeError.into_error()),
+ _ => Err(TTEK::HeaderReadError.into_error())
+ })
+ }
+
+ fn set_start_datetime(&mut self, dt: NaiveDateTime) -> Result<()> {
+ let s = dt.format(DATE_TIME_FORMAT).to_string();
+
+ self.get_header_mut()
+ .insert(DATE_TIME_START_HEADER_PATH, Value::String(s))
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| ())
+ }
+
+ fn get_start_datetime(&self) -> Result<Option<NaiveDateTime>> {
+ self.get_header()
+ .read(DATE_TIME_START_HEADER_PATH)
+ .map_err_into(TTEK::HeaderReadError)
+ .and_then(header_value_to_dt)
+ }
+
+ fn delete_start_datetime(&mut self) -> Result<()> {
+ self.get_header_mut()
+ .delete(DATE_TIME_START_HEADER_PATH)
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| ())
+ }
+
+ fn set_end_datetime(&mut self, dt: NaiveDateTime) -> Result<()> {
+ let s = dt.format(DATE_TIME_FORMAT).to_string();
+
+ self.get_header_mut()
+ .insert(DATE_TIME_END_HEADER_PATH, Value::String(s))
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| ())
+ }
+
+ fn get_end_datetime(&self) -> Result<Option<NaiveDateTime>> {
+ self.get_header()
+ .read(DATE_TIME_END_HEADER_PATH)
+ .map_err_into(TTEK::HeaderReadError)
+ .and_then(header_value_to_dt)
+ }
+
+ fn delete_end_datetime(&mut self) -> Result<()> {
+ self.get_header_mut()
+ .delete(DATE_TIME_END_HEADER_PATH)
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| ())
+ }
+
+ /// Check whether the Event is valid
+ ///
+ /// That is:
+ ///
+ /// * The end date is after the start date (or not set)
+ ///
+ /// # Return values
+ ///
+ /// Ok(true) if Event is valid
+ /// Ok(false) if Event is invalid
+ /// Err(e) if checking validity failed
+ ///
+ fn valid(&self) -> Result<bool> {
+ self.get_start_datetime().and_then(|st| self.get_end_datetime().map(|et| st <= et))
+ }
+
+}
+
+fn header_value_to_dt(val: Option<&Value>) -> Result<Option<NaiveDateTime>> {
+ match val {
+ Some(&Value::String(ref s)) => {
+ NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT)
+ .map_err_into(TTEK::DateTimeParserError)
+ .map(Some)
+
+ },
+ Some(_) => Err(TTEK::HeaderFieldTypeError.into_error()),
+ None => Ok(None),
+ }
+}
+
diff --git a/lib/entry/libimagentrytimetrack/src/timetrackingstore.rs b/lib/entry/libimagentrytimetrack/src/timetrackingstore.rs
new file mode 100644
index 0000000..94e993a
--- /dev/null
+++ b/lib/entry/libimagentrytimetrack/src/timetrackingstore.rs
@@ -0,0 +1,114 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// 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;
+
+use libimagstore::store::Store;
+use libimagstore::store::FileLockEntry;
+use libimagentrydatetime::datepath::compiler::DatePathCompiler;
+
+use result::Result;
+use constants::*;
+use error::TimeTrackErrorKind as TTEK;
+use error::MapErrInto;
+use iter::get::GetTimeTrackIter;
+
+use tag::TimeTrackingTag as TTT;
+
+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>>;
+
+ fn get_timetrackings(&'a self) -> Result<GetTimeTrackIter<'a>>;
+}
+
+fn now() -> NDT {
+ use chrono::offset::Local;
+ 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>> {
+ use std::path::PathBuf;
+
+ COMPILER.compile(CRATE_NAME, start)
+ .map_err_into(TTEK::StoreIdError)
+ .map(|mut id| {
+ id.local_push(PathBuf::from(ts.as_str()));
+ id
+ })
+ .and_then(|id| self.create(id).map_err_into(TTEK::StoreWriteError))
+ .and_then(|mut fle| {
+ let v = Value::String(ts.as_str().to_owned());
+ fle.get_header_mut()
+ .insert(DATE_TIME_TAG_HEADER_PATH, v)
+ .map_err_into(TTEK::HeaderWriteError)
+ .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)
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| fle)
+ })
+ }
+
+ 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)
+ .map_err_into(TTEK::HeaderWriteError)
+ .map(|_| fle)
+ })
+ }
+
+ fn get_timetrackings(&'a self) -> Result<GetTimeTrackIter<'a>> {
+ self.retrieve_for_module(CRATE_NAME)
+ .map_err_into(TTEK::StoreReadError)
+ .map(|iter| GetTimeTrackIter::new(iter, self))
+ }
+
+}
+