Merge pull request #966 from matthiasbeyer/libimagtimetrack
imag-timetrack / libimagtimetrack
This commit is contained in:
commit
b3e7f095ce
17 changed files with 943 additions and 0 deletions
|
@ -33,6 +33,7 @@ members = [
|
|||
"libimagref",
|
||||
"libimagrt",
|
||||
"libimagstore",
|
||||
"libimagtimetrack",
|
||||
"libimagtimeui",
|
||||
"libimagtodo",
|
||||
"libimagutil",
|
||||
|
|
66
doc/src/05100-lib-timetrack.md
Normal file
66
doc/src/05100-lib-timetrack.md
Normal file
|
@ -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.
|
||||
|
34
libimagtimetrack/Cargo.toml
Normal file
34
libimagtimetrack/Cargo.toml
Normal file
|
@ -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"
|
||||
|
1
libimagtimetrack/README.md
Symbolic link
1
libimagtimetrack/README.md
Symbolic link
|
@ -0,0 +1 @@
|
|||
../doc/src/05100-lib-timetrack.md
|
25
libimagtimetrack/src/constants.rs
Normal file
25
libimagtimetrack/src/constants.rs
Normal file
|
@ -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";
|
||||
|
39
libimagtimetrack/src/error.rs
Normal file
39
libimagtimetrack/src/error.rs
Normal file
|
@ -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;
|
||||
|
135
libimagtimetrack/src/event.rs
Normal file
135
libimagtimetrack/src/event.rs
Normal file
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
115
libimagtimetrack/src/eventstore.rs
Normal file
115
libimagtimetrack/src/eventstore.rs
Normal file
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
||||
|
77
libimagtimetrack/src/iter/create.rs
Normal file
77
libimagtimetrack/src/iter/create.rs
Normal file
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
65
libimagtimetrack/src/iter/get.rs
Normal file
65
libimagtimetrack/src/iter/get.rs
Normal file
|
@ -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)
|
||||
// }
|
||||
// }
|
||||
//
|
62
libimagtimetrack/src/iter/mod.rs
Normal file
62
libimagtimetrack/src/iter/mod.rs
Normal file
|
@ -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.
|
||||
}
|
||||
}
|
||||
|
65
libimagtimetrack/src/iter/setendtime.rs
Normal file
65
libimagtimetrack/src/iter/setendtime.rs
Normal file
|
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
75
libimagtimetrack/src/iter/storeid.rs
Normal file
75
libimagtimetrack/src/iter/storeid.rs
Normal file
|
@ -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()))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
55
libimagtimetrack/src/iter/tag.rs
Normal file
55
libimagtimetrack/src/iter/tag.rs
Normal file
|
@ -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())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
43
libimagtimetrack/src/lib.rs
Normal file
43
libimagtimetrack/src/lib.rs
Normal file
|
@ -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");
|
||||
|
26
libimagtimetrack/src/result.rs
Normal file
26
libimagtimetrack/src/result.rs
Normal file
|
@ -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>;
|
||||
|
||||
|
59
libimagtimetrack/src/tag.rs
Normal file
59
libimagtimetrack/src/tag.rs
Normal file
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue