tackler_api/
filters.rs

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright 2023 E257.FI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

//! Transaction filters
//!
//! The filtering logic is implemented in [`tackler-core`].
//!
//! [`tackler-core`]: ../../tackler_core/index.html
mod filter_definition;
pub mod logic;
pub mod posting;
pub mod txn;

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Error, Formatter};

pub use crate::filters::filter_definition::FilterDefinition;

use logic::TxnFilterAND;
use logic::TxnFilterNOT;
use logic::TxnFilterOR;

use txn::TxnFilterBBoxLatLon;
use txn::TxnFilterBBoxLatLonAlt;
use txn::TxnFilterTxnCode;
use txn::TxnFilterTxnComments;
use txn::TxnFilterTxnDescription;
use txn::TxnFilterTxnTSBegin;
use txn::TxnFilterTxnTSEnd;
use txn::TxnFilterTxnTags;
use txn::TxnFilterTxnUUID;

use posting::TxnFilterPostingAccount;
use posting::TxnFilterPostingAmountEqual;
use posting::TxnFilterPostingAmountGreater;
use posting::TxnFilterPostingAmountLess;
use posting::TxnFilterPostingComment;
use posting::TxnFilterPostingCommodity;

/// fmt with prefix indent
///
pub trait IndentDisplay {
    /// format with indent
    fn i_fmt(&self, indent: &str, f: &mut Formatter<'_>) -> std::fmt::Result;
}

/// Enum of all Transaction filters.
///
/// See [logic], [txn] and [posting] modules
/// for the documentation of transaction filters.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum TxnFilter {
    // Nullary test filters
    #[doc(hidden)]
    NullaryTRUE(NullaryTRUE),
    #[doc(hidden)]
    NullaryFALSE(NullaryFALSE),

    // Logic filters
    #[doc(hidden)]
    TxnFilterAND(TxnFilterAND),
    #[doc(hidden)]
    TxnFilterOR(TxnFilterOR),
    #[doc(hidden)]
    TxnFilterNOT(TxnFilterNOT),

    // TXN Header filters
    #[doc(hidden)]
    TxnFilterTxnTSBegin(TxnFilterTxnTSBegin),
    #[doc(hidden)]
    TxnFilterTxnTSEnd(TxnFilterTxnTSEnd),
    #[doc(hidden)]
    TxnFilterTxnCode(TxnFilterTxnCode),
    #[doc(hidden)]
    TxnFilterTxnDescription(TxnFilterTxnDescription),
    #[doc(hidden)]
    TxnFilterTxnUUID(TxnFilterTxnUUID),
    #[doc(hidden)]
    TxnFilterBBoxLatLon(TxnFilterBBoxLatLon),
    #[doc(hidden)]
    TxnFilterBBoxLatLonAlt(TxnFilterBBoxLatLonAlt),
    #[doc(hidden)]
    TxnFilterTxnTags(TxnFilterTxnTags),
    #[doc(hidden)]
    TxnFilterTxnComments(TxnFilterTxnComments),

    // TXN Postings
    #[doc(hidden)]
    TxnFilterPostingAccount(TxnFilterPostingAccount),
    #[doc(hidden)]
    TxnFilterPostingComment(TxnFilterPostingComment),
    #[doc(hidden)]
    TxnFilterPostingAmountEqual(TxnFilterPostingAmountEqual),
    #[doc(hidden)]
    TxnFilterPostingAmountLess(TxnFilterPostingAmountLess),
    #[doc(hidden)]
    TxnFilterPostingAmountGreater(TxnFilterPostingAmountGreater),
    #[doc(hidden)]
    TxnFilterPostingCommodity(TxnFilterPostingCommodity),
}

impl Display for TxnFilter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.i_fmt("", f)
    }
}

impl IndentDisplay for TxnFilter {
    fn i_fmt(&self, indent: &str, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            // specials
            TxnFilter::NullaryTRUE(tf) => tf.i_fmt(indent, f),
            TxnFilter::NullaryFALSE(tf) => tf.i_fmt(indent, f),

            // logic filters
            TxnFilter::TxnFilterAND(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterOR(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterNOT(tf) => tf.i_fmt(indent, f),

            // txn header filters
            TxnFilter::TxnFilterTxnTSBegin(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterTxnTSEnd(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterTxnCode(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterTxnDescription(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterTxnUUID(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterBBoxLatLon(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterBBoxLatLonAlt(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterTxnTags(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterTxnComments(tf) => tf.i_fmt(indent, f),

            // posting filters
            TxnFilter::TxnFilterPostingAccount(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterPostingComment(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterPostingAmountEqual(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterPostingAmountLess(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterPostingAmountGreater(tf) => tf.i_fmt(indent, f),
            TxnFilter::TxnFilterPostingCommodity(tf) => tf.i_fmt(indent, f),
        }
    }
}

/// Special always true filter (e.g. selects always)
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NullaryTRUE {}

impl IndentDisplay for NullaryTRUE {
    fn i_fmt(&self, indent: &str, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{indent}All pass")
    }
}

/// Special always false filter (e.g. selects nothing)
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NullaryFALSE {}

impl IndentDisplay for NullaryFALSE {
    fn i_fmt(&self, indent: &str, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "{indent}None pass")
    }
}

fn logic_filter_indent_fmt(
    op: &str,
    indent: &str,
    filters: &[TxnFilter],
    f: &mut Formatter<'_>,
) -> std::fmt::Result {
    let new_ident = format!("{indent}  ");

    writeln!(f, "{indent}{op}")?;
    let result: Result<Vec<()>, Error> = filters.iter().map(|tf| tf.i_fmt(&new_ident, f)).collect();

    match result {
        Ok(_) => Ok(()),
        Err(err) => Err(err),
    }
}

fn posting_filter_indent_fmt(
    indent: &str,
    target: &str,
    regex: &str,
    op: &str,
    amount: &Decimal,
    f: &mut Formatter<'_>,
) -> std::fmt::Result {
    let my_indent = format!("{indent}  ");
    writeln!(f, "{indent}{target}")?;
    writeln!(f, "{my_indent}account: \"{regex}\"")?;
    writeln!(f, "{my_indent}amount {op} {amount}")
}