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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::time::Duration;

use serde::{Deserialize, Serialize};

use crate::cache::CardCache;
use crate::categories::Category;
use crate::{deserialize_duration, serialize_duration, Id};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FilterUtil {
    pub allowed_cards: Vec<Id>, // Overrides all other filters.
    pub disallowed_cards: Vec<Id>,
    pub allowed_categories: Vec<Category>,
    pub excluded_categories: Vec<Category>,
    pub contains: Option<String>,
    pub tags: Vec<String>,
    pub suspended: Option<bool>,
    pub resolved: Option<bool>,
    pub pending: Option<bool>,
    pub finished: Option<bool>,
    #[serde(
        serialize_with = "serialize_duration",
        deserialize_with = "deserialize_duration"
    )]
    pub max_strength: Option<Duration>,
    #[serde(
        serialize_with = "serialize_duration",
        deserialize_with = "deserialize_duration"
    )]
    pub min_strength: Option<Duration>,
    #[serde(
        serialize_with = "serialize_duration",
        deserialize_with = "deserialize_duration"
    )]
    pub max_stability: Option<Duration>,
    #[serde(
        serialize_with = "serialize_duration",
        deserialize_with = "deserialize_duration"
    )]
    pub min_stability: Option<Duration>,
    pub max_recall_rate: Option<f32>,
    pub min_recall_rate: Option<f32>,
    pub max_lapses: Option<u32>,
    pub all_dependencies: Option<Box<Self>>,
    pub any_dependencies: Option<Box<Self>>,
    pub all_dependents: Option<Box<Self>>,
    pub any_dependents: Option<Box<Self>>,
}

impl FilterUtil {
    pub fn new_permissive() -> Self {
        Self::default()
    }

    pub fn new_review() -> Self {
        FilterUtil {
            max_recall_rate: Some(0.9),
            resolved: Some(true),
            suspended: Some(false),
            finished: Some(true),
            ..Default::default()
        }
    }

    pub fn evaluate_cards(&self, cards: Vec<Id>, cache: &mut CardCache) -> Vec<Id> {
        cards
            .into_iter()
            .filter(|card| self.keep_card(*card, cache))
            .collect()
    }

    pub fn keep_card(&self, card: Id, cache: &mut CardCache) -> bool {
        let card = cache.get_ref(card);

        if self.allowed_cards.contains(&card.id()) {
            return true;
        };

        if self.disallowed_cards.contains(&card.id()) {
            return false;
        };

        if !self.allowed_categories.is_empty()
            && !self
                .allowed_categories
                .iter()
                .any(|cat| card.category() == cat)
        {
            return false;
        }

        if self
            .excluded_categories
            .iter()
            .any(|cat| card.category() == cat)
        {
            return false;
        }

        if let Some(suspended) = self.suspended {
            if card.is_suspended() != suspended {
                return false;
            }
        }

        if let Some(resolved) = self.resolved {
            if card.is_resolved(cache) != resolved {
                return false;
            }
        }

        if let Some(finished) = self.finished {
            if card.is_finished() != finished {
                return false;
            }
        }
        if let Some(pending) = self.pending {
            if card.is_pending() != pending {
                return false;
            }
        }

        if let Some(max_strength) = self.max_strength {
            if !card
                .strength()
                .map(|card_strength| card_strength < max_strength)
                .unwrap_or(false)
            {
                return false;
            }
        }

        if let Some(min_strength) = self.min_strength {
            if !card
                .strength()
                .map(|card_strength| card_strength > min_strength)
                .unwrap_or(false)
            {
                return false;
            }
        }

        if let Some(max_stability) = self.max_stability {
            if !card
                .stability()
                .map(|card_stability| card_stability < max_stability)
                .unwrap_or(false)
            {
                return false;
            }
        }

        if let Some(min_stability) = self.min_stability {
            if !card
                .stability()
                .map(|card_stability| card_stability > min_stability)
                .unwrap_or(false)
            {
                return false;
            }
        }

        if let Some(max_recall_rate) = self.max_recall_rate {
            if !card
                .recall_rate()
                .map(|card_recall_rate| card_recall_rate < max_recall_rate)
                .unwrap_or(false)
            {
                return false;
            }
        }

        if let Some(min_recall_rate) = self.min_recall_rate {
            if !card
                .recall_rate()
                .map(|card_recall_rate| card_recall_rate > min_recall_rate)
                .unwrap_or(false)
            {
                return false;
            }
        }

        if let Some(search) = self.contains.as_deref() {
            if !(card.front_text().contains(search) || card.back_text().contains(search)) {
                return false;
            }
        }

        if !self.tags.is_empty() && !self.tags.iter().any(|tag| card.contains_tag(tag)) {
            return false;
        }

        // no cyclical dependencies need to hold true to avoid stack overflow.
        if let Some(all_dependencies) = &self.all_dependencies {
            let dependencies = cache.dependencies(card.id());
            if !dependencies.is_empty()
                && !dependencies
                    .iter()
                    .all(|card| all_dependencies.keep_card(*card, cache))
            {
                return false;
            }
        }

        if let Some(all_dependents) = &self.any_dependents {
            let dependents = cache.dependents(card.id());
            if !dependents.is_empty()
                && !dependents
                    .iter()
                    .all(|card| all_dependents.keep_card(*card, cache))
            {
                return false;
            }
        }

        if let Some(any_dependents) = &self.any_dependents {
            let dependents = cache.dependents(card.id());
            if !dependents
                .iter()
                .any(|card| any_dependents.keep_card(*card, cache))
            {
                return false;
            }
        }

        if let Some(any_depencies) = &self.any_dependencies {
            let dependencies = cache.dependencies(card.id());
            if dependencies
                .iter()
                .any(|card| any_depencies.keep_card(*card, cache))
            {
                return false;
            }
        }

        true
    }
}