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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! This module contains the SelectionEngine that handles user file selection with precedence rules.
//!
//! The SelectionEngine implements the A,A',B,B' system where:
//! - A, B: Base patterns (handled by FilterEngine)
//! - A', B': User actions with precedence rules (specific > generic, recent > old)
use crate::filter::FilterEngine;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
/// Represents a user action on a file or directory
#[derive(Debug, Clone)]
pub struct SelectionAction {
pub path: PathBuf,
pub action: ActionType,
pub timestamp: SystemTime,
pub specificity: u32, // Higher = more specific (more path components)
}
/// Type of selection action
#[derive(Debug, Clone, PartialEq)]
pub enum ActionType {
Include,
Exclude,
}
/// SelectionEngine handles both pattern-based filtering and user actions
/// with clear precedence rules: specific > generic, recent > old
#[derive(Clone)]
pub struct SelectionEngine {
/// Base pattern filtering (A, B in A,A',B,B' system)
filter_engine: FilterEngine,
/// User actions (A', B' in A,A',B,B' system)
user_actions: Vec<SelectionAction>,
/// Cache for performance
cache: HashMap<PathBuf, bool>,
}
impl SelectionEngine {
/// Create a new SelectionEngine with base patterns
pub fn new(include_patterns: Vec<String>, exclude_patterns: Vec<String>) -> Self {
Self {
filter_engine: FilterEngine::new(&include_patterns, &exclude_patterns),
user_actions: Vec::new(),
cache: HashMap::new(),
}
}
/// The core decision method: determines if a file should be selected
/// Uses precedence rules: specific > generic, recent > old
pub fn is_selected(&mut self, path: &Path) -> bool {
// Check cache first for performance
if let Some(&cached) = self.cache.get(path) {
return cached;
}
let result = self.compute_selection(path);
self.cache.insert(path.to_path_buf(), result);
result
}
/// Compute selection without caching
fn compute_selection(&self, path: &Path) -> bool {
// Rule 1: Find the most specific and recent user action
if let Some(action) = self.find_applicable_user_action(path) {
return action.action == ActionType::Include;
}
// Rule 2: Fall back to existing FilterEngine logic (A, B)
if self.filter_engine.has_include_patterns() {
// If there are include patterns, use them
self.filter_engine.matches_patterns(path)
} else {
// No include patterns: default behavior is to include all files
// (unless excluded by exclude patterns)
!self.filter_engine.is_excluded(path)
}
}
/// Find the most applicable user action using precedence rules
fn find_applicable_user_action(&self, path: &Path) -> Option<&SelectionAction> {
let applicable_actions: Vec<&SelectionAction> = self
.user_actions
.iter()
.filter(|action| self.action_applies_to_path(action, path))
.collect();
if applicable_actions.is_empty() {
return None;
}
// Apply precedence rules: specific > generic, recent > old
applicable_actions.into_iter().max_by(|a, b| {
// First compare specificity (higher is better)
match a.specificity.cmp(&b.specificity) {
std::cmp::Ordering::Equal => {
// If same specificity, compare timestamp (more recent is better)
a.timestamp.cmp(&b.timestamp)
}
other => other,
}
})
}
/// Check if a user action applies to a given path
fn action_applies_to_path(&self, action: &SelectionAction, path: &Path) -> bool {
// Exact match
if action.path == path {
return true;
}
// Directory action applies to all children
if path.starts_with(&action.path) {
return true;
}
false
}
/// Calculate specificity score for a path (more components = more specific)
fn calculate_specificity(&self, path: &Path) -> u32 {
path.components().count() as u32
}
/// User interaction: include a file or directory
pub fn include_file(&mut self, path: PathBuf) {
self.add_user_action(path, ActionType::Include);
}
/// User interaction: exclude a file or directory
pub fn exclude_file(&mut self, path: PathBuf) {
self.add_user_action(path, ActionType::Exclude);
}
/// User interaction: toggle selection state
pub fn toggle_file(&mut self, path: PathBuf) {
let current_state = self.is_selected(&path);
let new_action = if current_state {
ActionType::Exclude
} else {
ActionType::Include
};
self.add_user_action(path, new_action);
}
/// Add a user action with timestamp and specificity
fn add_user_action(&mut self, path: PathBuf, action: ActionType) {
let specificity = self.calculate_specificity(&path);
let user_action = SelectionAction {
path,
action,
timestamp: SystemTime::now(),
specificity,
};
self.user_actions.push(user_action);
self.cache.clear(); // Invalidate cache when actions change
}
/// Get all currently selected files by scanning the filesystem
pub fn get_selected_files(&mut self, root_path: &Path) -> Result<Vec<PathBuf>, std::io::Error> {
// If we have user actions, return files based on those actions
if !self.user_actions.is_empty() {
let mut selected = Vec::new();
// Clone the actions to avoid borrow checker issues
let actions = self.user_actions.clone();
// Collect files from user actions that are includes
for action in &actions {
if action.action == ActionType::Include {
// Check if this action is still the winning action for this path
if self.is_selected(&action.path) {
selected.push(action.path.clone());
}
}
}
// Remove duplicates and sort
selected.sort();
selected.dedup();
return Ok(selected);
}
// Otherwise, scan filesystem for pattern matches
let mut selected = Vec::new();
self.collect_selected_files_recursive(root_path, root_path, &mut selected)?;
Ok(selected)
}
/// Recursively collect selected files
fn collect_selected_files_recursive(
&mut self,
root_path: &Path,
current_dir: &Path,
selected: &mut Vec<PathBuf>,
) -> Result<(), std::io::Error> {
for entry in std::fs::read_dir(current_dir)? {
let entry = entry?;
let path = entry.path();
// Convert to relative path for selection checking
let relative_path = if let Ok(rel) = path.strip_prefix(root_path) {
rel
} else {
continue;
};
if self.is_selected(relative_path) {
if path.is_file() {
selected.push(relative_path.to_path_buf());
} else if path.is_dir() {
// Recursively check subdirectories
self.collect_selected_files_recursive(root_path, &path, selected)?;
}
}
}
Ok(())
}
/// Clear all user actions (reset to pattern-only behavior)
pub fn clear_user_actions(&mut self) {
self.user_actions.clear();
self.cache.clear();
}
/// Get the number of user actions
pub fn user_action_count(&self) -> usize {
self.user_actions.len()
}
/// Check if there are any user actions
pub fn has_user_actions(&self) -> bool {
!self.user_actions.is_empty()
}
/// Get access to the underlying filter engine
pub fn filter_engine(&self) -> &FilterEngine {
&self.filter_engine
}
}
impl std::fmt::Debug for SelectionEngine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SelectionEngine")
.field("filter_engine", &self.filter_engine)
.field("user_actions", &self.user_actions)
.field("cache_size", &self.cache.len())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_specificity_calculation() {
let engine = SelectionEngine::new(vec![], vec![]);
assert_eq!(engine.calculate_specificity(Path::new("file.rs")), 1);
assert_eq!(engine.calculate_specificity(Path::new("src/main.rs")), 2);
assert_eq!(
engine.calculate_specificity(Path::new("src/utils/helper.rs")),
3
);
}
#[test]
fn test_precedence_rules() {
let mut engine = SelectionEngine::new(vec![], vec![]);
// Add less specific action first
engine.exclude_file(PathBuf::from("src"));
// Add more specific action later
engine.include_file(PathBuf::from("src/main.rs"));
// More specific should win
assert!(!engine.is_selected(Path::new("src/lib.rs"))); // Excluded by src/
assert!(engine.is_selected(Path::new("src/main.rs"))); // Included specifically
}
#[test]
fn test_recent_wins_over_old() {
let mut engine = SelectionEngine::new(vec![], vec![]);
// First action
engine.exclude_file(PathBuf::from("main.rs"));
assert!(!engine.is_selected(Path::new("main.rs")));
// More recent action with same specificity
engine.include_file(PathBuf::from("main.rs"));
assert!(engine.is_selected(Path::new("main.rs")));
}
}