Merge pull request #998 from matthiasbeyer/libimagtimetrack/more-features
Libimagtimetrack/more features
This commit is contained in:
commit
3c07f47c4a
6 changed files with 156 additions and 3 deletions
141
libimagentrytimetrack/src/iter/filter.rs
Normal file
141
libimagentrytimetrack/src/iter/filter.rs
Normal file
|
@ -0,0 +1,141 @@
|
|||
//
|
||||
// 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 libimagstore::store::FileLockEntry;
|
||||
|
||||
use tag::TimeTrackingTag as TTT;
|
||||
use timetracking::TimeTracking;
|
||||
|
||||
pub struct WithOneOf<'a, I>
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>
|
||||
{
|
||||
iter: I,
|
||||
allowed_tags: &'a Vec<TTT>,
|
||||
}
|
||||
|
||||
impl<'a, I> WithOneOf<'a, I>
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>
|
||||
{
|
||||
|
||||
pub fn new(iter: I, allowed_tags: &'a Vec<TTT>) -> WithOneOf<'a, I> {
|
||||
WithOneOf {
|
||||
iter: iter,
|
||||
allowed_tags: allowed_tags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I> Iterator for WithOneOf<'a, I>
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>
|
||||
{
|
||||
type Item = Result<FileLockEntry<'a>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
match self.iter.next() {
|
||||
Some(Ok(fle)) => {
|
||||
match fle.get_timetrack_tag() {
|
||||
Err(e) => return Some(Err(e)),
|
||||
Ok(t) => if self.allowed_tags.contains(&t) {
|
||||
return Some(Ok(fle))
|
||||
} else {
|
||||
// loop
|
||||
},
|
||||
}
|
||||
},
|
||||
Some(Err(e)) => return Some(Err(e)),
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WithOneOfTags<'a> : Sized + Iterator<Item = Result<FileLockEntry<'a>>> {
|
||||
fn with_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithOneOf<'a, Self>;
|
||||
}
|
||||
|
||||
impl<'a, I> WithOneOfTags<'a> for I
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>,
|
||||
Self: Sized
|
||||
{
|
||||
fn with_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithOneOf<'a, Self> {
|
||||
WithOneOf::new(self, tags)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct WithNoneOf<'a, I>
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>
|
||||
{
|
||||
iter: I,
|
||||
disallowed_tags: &'a Vec<TTT>,
|
||||
}
|
||||
|
||||
impl<'a, I> WithNoneOf<'a, I>
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>
|
||||
{
|
||||
|
||||
pub fn new(iter: I, disallowed_tags: &'a Vec<TTT>) -> WithNoneOf<'a, I> {
|
||||
WithNoneOf {
|
||||
iter: iter,
|
||||
disallowed_tags: disallowed_tags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I> Iterator for WithNoneOf<'a, I>
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>
|
||||
{
|
||||
type Item = Result<FileLockEntry<'a>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
loop {
|
||||
match self.iter.next() {
|
||||
Some(Ok(fle)) => {
|
||||
match fle.get_timetrack_tag() {
|
||||
Err(e) => return Some(Err(e)),
|
||||
Ok(t) => if !self.disallowed_tags.contains(&t) {
|
||||
return Some(Ok(fle))
|
||||
} else {
|
||||
// loop
|
||||
},
|
||||
}
|
||||
},
|
||||
Some(Err(e)) => return Some(Err(e)),
|
||||
None => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WithNoneOfTags<'a> : Sized + Iterator<Item = Result<FileLockEntry<'a>>> {
|
||||
fn without_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithNoneOf<'a, Self>;
|
||||
}
|
||||
|
||||
impl<'a, I> WithNoneOfTags<'a> for I
|
||||
where I: Iterator<Item = Result<FileLockEntry<'a>>>,
|
||||
Self: Sized
|
||||
{
|
||||
fn without_timetracking_tags(self, tags: &'a Vec<TTT>) -> WithNoneOf<'a, Self> {
|
||||
WithNoneOf::new(self, tags)
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,7 @@
|
|||
//
|
||||
|
||||
pub mod create;
|
||||
pub mod filter;
|
||||
pub mod get;
|
||||
pub mod setendtime;
|
||||
pub mod storeid;
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
// 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::*;
|
||||
|
|
|
@ -25,6 +25,7 @@ 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 {
|
||||
|
|
|
@ -29,6 +29,7 @@ use chrono::naive::datetime::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;
|
||||
|
@ -41,6 +42,8 @@ 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>>;
|
||||
|
@ -59,6 +62,16 @@ pub trait TimeTracking {
|
|||
|
||||
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)
|
||||
.map(|value| match value {
|
||||
Some(&Value::String(ref s)) => s.clone().into(),
|
||||
_ => unimplemented!(),
|
||||
})
|
||||
}
|
||||
|
||||
fn set_start_datetime(&mut self, dt: NaiveDateTime) -> Result<()> {
|
||||
let s = dt.format(DATE_TIME_FORMAT).to_string();
|
||||
|
||||
|
|
|
@ -28,7 +28,6 @@ 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;
|
||||
|
|
Loading…
Reference in a new issue