2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-05-30 18:25:00 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
|
|
|
use std::fmt::{Debug, Formatter, Error as FmtError};
|
|
|
|
use std::result::Result as RResult;
|
|
|
|
|
2018-03-02 20:25:48 +00:00
|
|
|
use filters::filter::Filter;
|
|
|
|
|
2017-05-30 18:25:00 +00:00
|
|
|
use libimagstore::storeid::StoreIdIterator;
|
2018-03-02 20:25:48 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::is_in_diary::IsInDiary;
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::Fallible as Result;
|
2019-08-27 08:46:01 +00:00
|
|
|
|
2018-10-30 17:40:51 +00:00
|
|
|
use failure::err_msg;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
/// A iterator for iterating over diary entries
|
2018-03-02 20:25:48 +00:00
|
|
|
pub struct DiaryEntryIterator {
|
2017-08-28 19:58:43 +00:00
|
|
|
name: String,
|
2017-05-30 18:25:00 +00:00
|
|
|
iter: StoreIdIterator,
|
|
|
|
|
|
|
|
year: Option<i32>,
|
|
|
|
month: Option<u32>,
|
|
|
|
day: Option<u32>,
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:25:48 +00:00
|
|
|
impl Debug for DiaryEntryIterator {
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
|
|
|
|
write!(fmt, "DiaryEntryIterator<name = {}, year = {:?}, month = {:?}, day = {:?}>",
|
|
|
|
self.name, self.year, self.month, self.day)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:25:48 +00:00
|
|
|
impl DiaryEntryIterator {
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2018-03-02 20:25:48 +00:00
|
|
|
pub fn new(diaryname: String, iter: StoreIdIterator) -> DiaryEntryIterator {
|
2017-05-30 18:25:00 +00:00
|
|
|
DiaryEntryIterator {
|
|
|
|
name: diaryname,
|
2019-08-27 08:46:01 +00:00
|
|
|
iter,
|
2017-05-30 18:25:00 +00:00
|
|
|
|
|
|
|
year: None,
|
|
|
|
month: None,
|
|
|
|
day: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by year, get all diary entries for this year
|
2018-03-02 20:25:48 +00:00
|
|
|
pub fn year(mut self, year: i32) -> DiaryEntryIterator {
|
2017-05-30 18:25:00 +00:00
|
|
|
self.year = Some(year);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by month, get all diary entries for this month (every year)
|
2018-03-02 20:25:48 +00:00
|
|
|
pub fn month(mut self, month: u32) -> DiaryEntryIterator {
|
2017-05-30 18:25:00 +00:00
|
|
|
self.month = Some(month);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by day, get all diary entries for this day (every year, every year)
|
2018-03-02 20:25:48 +00:00
|
|
|
pub fn day(mut self, day: u32) -> DiaryEntryIterator {
|
2017-05-30 18:25:00 +00:00
|
|
|
self.day = Some(day);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:25:48 +00:00
|
|
|
impl Filter<StoreId> for DiaryEntryIterator {
|
|
|
|
fn filter(&self, id: &StoreId) -> bool {
|
|
|
|
if id.is_in_diary(&self.name) {
|
|
|
|
match (self.year, self.month, self.day) {
|
|
|
|
(None , None , None) => true,
|
|
|
|
(Some(y) , None , None) => id.is_in_collection(&[&self.name, &y.to_string()]),
|
|
|
|
(Some(y) , Some(m) , None) => id.is_in_collection(&[&self.name, &y.to_string(), &m.to_string()]),
|
|
|
|
(Some(y) , Some(m) , Some(d)) => id.is_in_collection(&[&self.name, &y.to_string(), &m.to_string(), &d.to_string()]),
|
|
|
|
(None , Some(_) , Some(_)) => false /* invalid case */,
|
|
|
|
(None , None , Some(_)) => false /* invalid case */,
|
|
|
|
(None , Some(_) , None) => false /* invalid case */,
|
|
|
|
(Some(_) , None , Some(_)) => false /* invalid case */,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for DiaryEntryIterator {
|
2018-04-30 15:28:54 +00:00
|
|
|
type Item = Result<StoreId>;
|
2017-05-30 18:25:00 +00:00
|
|
|
|
2017-08-29 08:52:10 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2017-05-30 18:25:00 +00:00
|
|
|
loop {
|
2018-03-02 20:25:48 +00:00
|
|
|
match self.iter.next() {
|
2018-04-30 15:28:54 +00:00
|
|
|
None => return None,
|
2018-10-30 17:40:51 +00:00
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
2018-04-30 15:28:54 +00:00
|
|
|
Some(Ok(s)) => {
|
2018-03-02 20:25:48 +00:00
|
|
|
debug!("Next element: {:?}", s);
|
|
|
|
if Filter::filter(self, &s) {
|
2018-04-30 15:28:54 +00:00
|
|
|
return Some(Ok(s))
|
2018-03-02 20:25:48 +00:00
|
|
|
} else {
|
|
|
|
continue
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
2018-03-02 20:25:48 +00:00
|
|
|
},
|
2017-05-30 18:25:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 19:58:43 +00:00
|
|
|
|
|
|
|
/// Get diary names.
|
|
|
|
///
|
|
|
|
/// # Warning
|
|
|
|
///
|
|
|
|
/// Does _not_ run a `unique` on the iterator!
|
|
|
|
pub struct DiaryNameIterator(StoreIdIterator);
|
|
|
|
|
|
|
|
impl DiaryNameIterator {
|
|
|
|
pub fn new(s: StoreIdIterator) -> DiaryNameIterator {
|
|
|
|
DiaryNameIterator(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for DiaryNameIterator {
|
|
|
|
type Item = Result<String>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2018-04-24 12:01:40 +00:00
|
|
|
while let Some(next) = self.0.next() {
|
2018-04-30 15:28:54 +00:00
|
|
|
match next {
|
2018-10-30 17:40:51 +00:00
|
|
|
Err(e) => return Some(Err(e)),
|
2018-04-30 15:28:54 +00:00
|
|
|
Ok(next) => if next.is_in_collection(&["diary"]) {
|
|
|
|
return Some(next
|
|
|
|
.to_str()
|
|
|
|
.and_then(|s| {
|
|
|
|
s.split("diary/")
|
|
|
|
.nth(1)
|
2019-08-27 08:46:01 +00:00
|
|
|
.and_then(|n| n.split('/').nth(0).map(String::from))
|
|
|
|
.ok_or_else(|| err_msg("Error finding diary name"))
|
2018-04-30 15:28:54 +00:00
|
|
|
}));
|
|
|
|
},
|
2018-04-24 12:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
2017-08-28 19:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|