libimagentryfilter/builtin/header/field_isempty.rs
1//
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18//
19
20use libimagstore::store::Entry;
21use toml_query::read::TomlValueReadExt;
22
23use crate::builtin::header::field_path::FieldPath;
24use filters::failable::filter::FailableFilter;
25
26use failure::Fallible as Result;
27use failure::Error;
28
29use toml::Value;
30
31pub struct FieldIsEmpty {
32 header_field_path: FieldPath,
33}
34
35impl FieldIsEmpty {
36
37 pub fn new(path: FieldPath) -> FieldIsEmpty {
38 FieldIsEmpty {
39 header_field_path: path,
40 }
41 }
42
43}
44
45impl FailableFilter<Entry> for FieldIsEmpty {
46 type Error = Error;
47
48 fn filter(&self, e: &Entry) -> Result<bool> {
49 Ok(e
50 .get_header()
51 .read(&self.header_field_path[..])?
52 .map(|v| {
53 match v {
54 Value::Array(ref a) => a.is_empty(),
55 Value::String(ref s) => s.is_empty(),
56 Value::Table(ref t) => t.is_empty(),
57 Value::Boolean(_) |
58 Value::Float(_) |
59 Value::Datetime(_) |
60 Value::Integer(_) => false,
61 }
62 })
63 .unwrap_or(true))
64 }
65
66}
67
68
69