summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-06-29 14:55:39 +0200
committerGitHub <noreply@github.com>2017-06-29 14:55:39 +0200
commitb3e7f095ce2b91255dd641e68009afba7582db53 (patch)
tree5b7bc3c252dbef35f8835424be744715d5fe5075
parentc71b70702c30f587272fc2dbb20344bbcdeac872 (diff)
parentdd7954ddb6c2db51017ffa93536bf2a4936be1b8 (diff)
downloadimag-b3e7f095ce2b91255dd641e68009afba7582db53.zip
imag-b3e7f095ce2b91255dd641e68009afba7582db53.tar.gz
Merge pull request #966 from matthiasbeyer/libimagtimetrack
imag-timetrack / libimagtimetrack
-rw-r--r--Cargo.toml1
-rw-r--r--doc/src/05100-lib-timetrack.md66
-rw-r--r--libimagtimetrack/Cargo.toml34
l---------libimagtimetrack/README.md1
-rw-r--r--libimagtimetrack/src/constants.rs25
-rw-r--r--libimagtimetrack/src/error.rs39
-rw-r--r--libimagtimetrack/src/event.rs135
-rw-r--r--libimagtimetrack/src/eventstore.rs115
-rw-r--r--libimagtimetrack/src/iter/create.rs77
-rw-r--r--libimagtimetrack/src/iter/get.rs65
-rw-r--r--libimagtimetrack/src/iter/mod.rs62
-rw-r--r--libimagtimetrack/src/iter/setendtime.rs65
-rw-r--r--libimagtimetrack/src/iter/storeid.rs75
-rw-r--r--libimagtimetrack/src/iter/tag.rs55
-rw-r--r--libimagtimetrack/src/lib.rs43
-rw-r--r--libimagtimetrack/src/result.rs26
-rw-r--r--libimagtimetrack/src/tag.rs59
17 files changed, 943 insertions, 0 deletions
diff --git a/Cargo.toml b/Cargo.toml
index f94a824..0dcce55 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -33,6 +33,7 @@ members = [
"libimagref",
"libimagrt",
"libimagstore",
+ "libimagtimetrack",
"libimagtimeui",
"libimagtodo",
"libimagutil",
diff --git a/doc/src/05100-lib-timetrack.md b/doc/src/05100-lib-timetrack.md
new file mode 100644
index 0000000..7dd5848
--- /dev/null
+++ b/doc/src/05100-lib-timetrack.md
@@ -0,0 +1,66 @@
+## libimagtimetrack
+
+A library for tracking time events in the imag store.
+
+### Store format
+
+Events are stored with a store id like this:
+
+```
+/timetrack/<insert-date-year>/<insert-date-month>/<insert-date-day>/<insert-date-time>-<tag>.ext
+```
+
+Timetrackings contain
+
+* a comment (optional, free text)
+* a start date
+* an end date
+* a tag
+
+by default and might be extended with more header fields as one likes.
+
+The header of a timetrack "work" entry looks like this:
+
+```toml
+[event]
+tag = "work"
+start = "2017-01-02T03:04:05"
+end = "2017-01-02T06:07:08"
+```
+
+Normal tags (as in `libimagentrytag`) are explicitely _not_ used for tagging,
+so the user has the possibility to use normal tags on these entries as well.
+
+The `tag` field is of type string, as for one tag, one entry is created. This
+way, one can track overlapping tags, as in:
+
+```bash
+imag timetrack start foo
+imag timetrack start bar
+imag timetrack stop foo
+imag timetrack start baz
+imag timetrack stop bar
+imag timetrack stop baz
+```
+
+The `end` field is, of course, only set if the event already ended.
+
+### Library functionality
+
+The library uses the `libimagentrydatetime::datepath::DatePathBuilder` for
+building `StoreId` objects.
+
+The library offers two central traits:
+
+* `TimeTrackStore`, which extends a `Store` object with functionality to
+ create `FileLockEntry` objects with a certain setting that is used for
+ time-tracking, and
+* `TimeTracking`, which extends `Entry` with helper functions to query the
+ entry-metadata that is used for the time tracking functionality
+
+The library does _not_ provide functionality to implement `imag-timetrack` or
+so, as the core functionality is already given and the commandline application
+can implement the missing bits in few lines of code.
+
+Aggregating functionality might be provided at a later point in time.
+
diff --git a/libimagtimetrack/Cargo.toml b/libimagtimetrack/Cargo.toml
new file mode 100644
index 0000000..f7eaad7
--- /dev/null
+++ b/libimagtimetrack/Cargo.toml
@@ -0,0 +1,34 @@
+[package]
+name = "libimagentrytimetrack"
+version = "0.3.0"
+authors = ["Matthias Beyer <mail@beyermatthias.de>"]
+
+description = "Library for the imag core distribution"
+
+keywords = ["imag", "PIM", "personal", "information", "management"]
+readme = "../README.md"
+license = "LGPL-2.1"
+
+documentation = "https://matthiasbeyer.github.io/imag/imag_documentation/index.html"
+repository = "https://github.com/matthiasbeyer/imag"
+homepage = "http://imag-pim.org"
+
+[dependencies]
+filters = "0.1"
+chrono = "0.3"
+toml = "0.4"
+toml-query = "0.2"
+lazy_static = "0.2"
+
+[dependencies.libimagerror]
+path = "../libimagerror"
+
+[dependencies.libimagstore]
+path = "../libimagstore"
+
+[dependencies.libimagentrydatetime]
+path = "../libimagentrydatetime"
+
+[dependencies.libimagentrytag]
+path = "../libimagentrytag"
+
diff --git a/libimagtimetrack/README.md b/libimagtimetrack/README.md
new file mode 120000
index 0000000..1a80613
--- /dev/null
+++ b/libimagtimetrack/README.md
@@ -0,0 +1 @@
+../doc/src/05100-lib-timetrack.md \ No newline at end of file
diff --git a/libimagtimetrack/src/constants.rs b/libimagtimetrack/src/constants.rs
new file mode 100644
index 0000000..63eeaf6
--- /dev/null
+++ b/libimagtimetrack/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/libimagtimetrack/src/error.rs b/libimagtimetrack/src/error.rs
new file mode 100644
index 0000000..3ac4a11
--- /dev/null
+++ b/libimagtimetrack/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/libimagtimetrack/src/event.rs b/libimagtimetrack/src/event.rs
new file mode 100644
index 0000000..40b6e34
--- /dev/null
+++ b/libimagtimetrack/src/event.rs
@@ -0,0 +1,135 @@
+//
+// 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::datetime::NaiveDateTime;
+
+use libimagstore::store::Entry;
+use libimagerror::into::IntoError;
+
+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 set_start_datetime(&mut self, dt: NaiveDateTime) -> Result<()>;
+
+ fn get_start_datetime(&self) -> Result<NaiveDateTime>;
+
+ fn delete_start_datetime(&mut self) -> Result<()>;
+
+ fn set_end_datetime(&mut self, dt: NaiveDateTime) -> Result<()>;
+
+ fn get_end_datetime(&self) -> Result<NaiveDateTime>;
+
+ fn delete_end_datetime(&mut self) -> Result<()>;
+
+ fn valid(&self) -> Result<bool>;
+
+}
+
+impl TimeTracking for Entry {
+
+ 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<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<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: &Value) -> Result<NaiveDateTime> {
+ match val {
+ &Value::String(ref s) => {
+ NaiveDateTime::parse_from_str(s, DATE_TIME_FORMAT)
+ .map_err_into(TTEK::DateTimeParserError)
+ },
+ _ => Err(TTEK::HeaderFieldTypeError.into_error())
+ }
+}
+
diff --git a/libimagtimetrack/src/eventstore.rs b/libimagtimetrack/src/eventstore.rs
new file mode 100644
index 0000000..1ce5d55
--- /dev/null
+++ b/libimagtimetrack/src/eventstore.rs
@@ -0,0 +1,115 @@
+//
+// 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 libimagstore::storeid::StoreId;
+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<I>(&'a self) -> Result<GetTimeTrackIter<'a>>;
+}
+
+fn now() -> NDT {
+ use chrono::offset::local::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<I>(&'a self) -> Result<GetTimeTrackIter<'a>> {
+ self.retrieve_for_module(CRATE_NAME)
+ .map_err_into(TTEK::StoreReadError)
+ .map(|iter| GetTimeTrackIter::new(iter, self))
+ }
+
+}
+
diff --git a/libimagtimetrack/src/iter/create.rs b/libimagtimetrack/src/iter/create.rs
new file mode 100644
index 0000000..bbaba19
--- /dev/null
+++ b/libimagtimetrack/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::datetime::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/libimagtimetrack/src/iter/get.rs b/libimagtimetrack/src/iter/get.rs
new file mode 100644
index 0000000..62e0a85
--- /dev/null
+++ b/libimagtimetrack/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/libimagtimetrack/src/iter/mod.rs b/libimagtimetrack/src/iter/mod.rs
new file mode 100644
index 0000000..22dcea2
--- /dev/null
+++ b/libimagtimetrack/src/iter/mod.rs
@@ -0,0 +1,62 @@
+//
+// 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 get;
+pub mod setendtime;
+pub mod storeid;
+pub mod tag;
+
+#[cfg(test)]
+mod test {
+ use chrono::naive::date::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/libimagtimetrack/src/iter/setendtime.rs b/libimagtimetrack/src/iter/setendtime.rs
new file mode 100644
index 0000000..3a48491
--- /dev/null
+++ b/libimagtimetrack/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::datetime::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/libimagtimetrack/src/iter/storeid.rs b/libimagtimetrack/src/iter/storeid.rs
new file mode 100644
index 0000000..ecab55d
--- /dev/null
+++ b/libimagtimetrack/src/iter/storeid.rs
@@ -0,0 +1,75 @@
+//
+// 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::path::PathBuf;
+
+use chrono::naive::datetime::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/libimagtimetrack/src/iter/tag.rs b/libimagtimetrack/src/iter/tag.rs
new file mode 100644
index 0000000..5f0c9d5
--- /dev/null
+++ b/libimagtimetrack/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::datetime::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/libimagtimetrack/src/lib.rs b/libimagtimetrack/src/lib.rs
new file mode 100644
index 0000000..44b088e
--- /dev/null
+++ b/libimagtimetrack/src/lib.rs
@@ -0,0 +1,43 @@
+//
+// 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 libimagerror;
+#[macro_use]
+extern crate libimagstore;
+extern crate libimagentrydatetime;
+extern crate libimagentrytag;
+
+mod constants;
+pub mod error;
+pub mod event;
+pub mod eventstore;
+pub mod iter;
+pub mod result;
+pub mod tag;
+
+module_entry_path_mod!("timetrack");
+
diff --git a/libimagtimetrack/src/result.rs b/libimagtimetrack/src/result.rs
new file mode 100644
index 0000000..8ea8173
--- /dev/null
+++ b/libimagtimetrack/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/libimagtimetrack/src/tag.rs b/libimagtimetrack/src/tag.rs
new file mode 100644
index 0000000..c28d19f
--- /dev/null
+++ b/libimagtimetrack/src/tag.rs
@@ -0,0 +1,59 @@
+//
+// 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::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.
+pub struct TimeTrackingTag(String);
+
+impl TimeTrackingTag {
+ pub fn as_str(&self) -> &str {
+ &self.0
+ }
+}
+
+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))
+ }
+}
+