~matthiasbeyer/imag

ref: 2a81ad4dc2f0d659bd344a663da1d901f8dcccd9 imag/lib/entry/libimagentrylink/src/iter.rs -rw-r--r-- 2.5 KiB
2a81ad4dMatthias Beyer Remove main() in doctest 1 year, 11 months ago
                                                                                                    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 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::vec::IntoIter;

use failure::Error;
use failure::ResultExt;
use failure::Fallible as Result;
use toml::Value;
use itertools::Itertools;

use libimagstore::store::Store;
use libimagstore::store::FileLockEntry;
use libimagerror::errors::ErrorMsg as EM;

use crate::link::Link;

pub struct LinkIter(IntoIter<Link>);

impl LinkIter {

    pub fn new(v: Vec<Link>) -> LinkIter {
        LinkIter(v.into_iter())
    }

    pub fn into_getter(self, store: &Store) -> GetIter {
        GetIter(self.0, store)
    }

}

impl Iterator for LinkIter {
    type Item = Link;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}



pub trait IntoValues {
    fn into_values(self) -> Vec<Result<Value>>;
}

impl<I: Iterator<Item = Link>> IntoValues for I {
    fn into_values(self) -> Vec<Result<Value>> {
        self.unique()
            .sorted() // Cannot sort toml::Value, hence uglyness here
            .map(|link| link.to_value().context(EM::ConversionError).map_err(Error::from))
            .collect()
    }
}

/// An Iterator that `Store::get()`s the Entries from the store while consumed
pub struct GetIter<'a>(IntoIter<Link>, &'a Store);

impl<'a> GetIter<'a> {
    pub fn new(i: IntoIter<Link>, store: &'a Store) -> GetIter<'a> {
        GetIter(i, store)
    }

    pub fn store(&self) -> &Store {
        self.1
    }
}

impl<'a> Iterator for GetIter<'a> {
    type Item = Result<FileLockEntry<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().and_then(|id| match self.1.get(id) {
            Ok(None)    => None,
            Ok(Some(x)) => Some(Ok(x)),
            Err(e)      => Some(Err(e).map_err(From::from)),
        })
    }

}