syncweb-core 0.1.1-alpha.4

delay-tolerant web surfing
Documentation
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Search and ranking APIs for local and synchronized entries.

use std::{
    path::{Path, PathBuf},
    time::SystemTime,
};

use regex::Regex;

use crate::{
    error::{Result, SyncwebError},
    fs::{FileEntry, FileType, ParallelScanner},
    parsing::{parse_relative_time, parse_size_constraint},
};

/// How a find expression is interpreted.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Default)]
#[non_exhaustive]
pub enum MatchKind {
    Exact,
    #[default]
    Glob,
    Regex,
}

/// Filters supported by [`FindEngine`].
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct FindQuery {
    pub pattern: String,
    pub kind: MatchKind,
    pub max_depth: Option<usize>,
    pub min_depth: Option<usize>,
    pub min_size: Option<u64>,
    pub max_size: Option<u64>,
    pub modified_after: Option<SystemTime>,
    pub modified_before: Option<SystemTime>,
    pub extension: Option<String>,
    pub extensions: Vec<String>,
    pub file_type: Option<FileType>,
    pub case_sensitive: Option<bool>,
    pub fixed_strings: bool,
    pub full_path: bool,
    pub hidden: bool,
    pub follow_links: bool,
    pub absolute_path: bool,
    pub downloadable: bool,
}

impl FindQuery {
    #[must_use]
    pub fn exact(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
            kind: MatchKind::Exact,
            ..Self::default()
        }
    }

    #[must_use]
    pub fn glob(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
            kind: MatchKind::Glob,
            ..Self::default()
        }
    }

    #[must_use]
    pub fn regex(pattern: impl Into<String>) -> Self {
        Self {
            pattern: pattern.into(),
            kind: MatchKind::Regex,
            ..Self::default()
        }
    }

    #[must_use]
    pub const fn depth(mut self, max_depth: usize) -> Self {
        self.max_depth = Some(max_depth);
        self
    }

    #[must_use]
    pub const fn size(mut self, min: Option<u64>, max: Option<u64>) -> Self {
        self.min_size = min;
        self.max_size = max;
        self
    }

    #[must_use]
    pub fn extension(mut self, extension: impl Into<String>) -> Self {
        self.extension = Some(extension.into().trim_start_matches('.').to_owned());
        self
    }

    #[must_use]
    pub fn extensions(mut self, exts: Vec<String>) -> Self {
        self.extensions = exts
            .into_iter()
            .map(|e| e.trim_start_matches('.').to_lowercase())
            .collect();
        self
    }

    #[must_use]
    pub const fn file_type(mut self, file_type: FileType) -> Self {
        self.file_type = Some(file_type);
        self
    }

    #[must_use]
    pub const fn modified_after(mut self, time: SystemTime) -> Self {
        self.modified_after = Some(time);
        self
    }

    #[must_use]
    pub const fn modified_before(mut self, time: SystemTime) -> Self {
        self.modified_before = Some(time);
        self
    }

    /// Parse size constraints from fd-find style strings.
    ///
    /// # Errors
    ///
    /// Returns an error if the string cannot be parsed.
    pub fn parse_size_constraints(size_strs: &[String]) -> Result<(Option<u64>, Option<u64>)> {
        let mut min_size: Option<u64> = None;
        let mut max_size: Option<u64> = None;

        for s in size_strs {
            let (min, max) = parse_size_constraint(s)?;
            if let Some(new_min) = min {
                min_size = Some(min_size.map_or(new_min, |current| current.max(new_min)));
            }
            if let Some(new_max) = max {
                max_size = Some(max_size.map_or(new_max, |current| current.min(new_max)));
            }
        }

        Ok((min_size, max_size))
    }

    /// Parse time constraints from human-readable strings.
    ///
    /// # Errors
    ///
    /// Returns an error if the string cannot be parsed.
    pub fn parse_time_constraints(
        within_strs: &[String],
        before_strs: &[String],
        modified_strs: &[String],
    ) -> Result<(Option<SystemTime>, Option<SystemTime>)> {
        let mut after: Option<SystemTime> = None;
        let mut before: Option<SystemTime> = None;

        // "3 days" means after = now - 3 days
        for s in within_strs {
            let time = parse_relative_time(&format!("-{s}"))?;
            after = Some(after.map_or(time, |current| if time > current { time } else { current }));
        }

        // "3 years" means before = now - 3 years (older than)
        for s in before_strs {
            let time = parse_relative_time(&format!("-{s}"))?;
            before = Some(before.map_or(time, |current| if time < current { time } else { current }));
        }

        // "-3 days" means after = now - 3 days (newer than)
        // "+3 days" means before = now - 3 days (older than)
        for s in modified_strs {
            let time = parse_relative_time(s)?;
            let now = SystemTime::now();
            if time < now {
                // Past time
                after = Some(after.map_or(time, |current| if time > current { time } else { current }));
            } else {
                // Future time (shouldn't happen, but handle gracefully)
                before = Some(before.map_or(time, |current| if time < current { time } else { current }));
            }
        }

        Ok((after, before))
    }
}

/// Recursive filesystem search engine.
#[derive(Clone)]
pub struct FindEngine {
    root: PathBuf,
    ignore_patterns: Vec<String>,
    threads: usize,
}

impl FindEngine {
    #[must_use]
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self {
            root: root.into(),
            ignore_patterns: Vec::new(),
            threads: 0,
        }
    }

    #[must_use]
    pub fn with_ignore_patterns(mut self, patterns: Vec<String>) -> Self {
        self.ignore_patterns = patterns;
        self
    }

    #[must_use]
    pub const fn with_threads(mut self, threads: usize) -> Self {
        self.threads = threads;
        self
    }

    /// Scan the root and apply a query.
    /// # Errors
    ///
    /// Returns an error if the filesystem cannot be accessed.
    pub fn find(&self, query: &FindQuery) -> Result<Vec<FileEntry>> {
        let scanner = ParallelScanner::new(&self.root, self.ignore_patterns.clone(), self.threads);
        let entries = if query.file_type.is_some_and(|kind| kind != FileType::File) {
            scanner.scan_all()?
        } else {
            scanner.scan()?
        };
        if query.kind == MatchKind::Regex {
            Regex::new(&query.pattern)
                .map_err(|error| SyncwebError::operation("invalid find regular expression", error))?;
        }
        Ok(filter_entries(&entries, query))
    }

    /// Apply a query to already indexed metadata without reading file content.
    #[must_use]
    pub fn filter(&self, entries: &[FileEntry], query: &FindQuery) -> Vec<FileEntry> {
        filter_entries(entries, query)
    }
}

/// Filter an existing metadata index.
#[must_use]
pub fn filter_entries(entries: &[FileEntry], query: &FindQuery) -> Vec<FileEntry> {
    let regex = (query.kind == MatchKind::Regex)
        .then(|| Regex::new(&query.pattern).ok())
        .flatten();
    entries
        .iter()
        .filter(|entry| {
            // Depth filtering
            if let Some(max_depth) = query.max_depth
                && entry.relative_path.components().count() > max_depth
            {
                return false;
            }
            if let Some(min_depth) = query.min_depth
                && entry.relative_path.components().count() < min_depth
            {
                return false;
            }

            // Size filtering
            if query.min_size.is_some_and(|size| entry.size < size)
                || query.max_size.is_some_and(|size| entry.size > size)
            {
                return false;
            }

            // Time filtering
            if query.modified_after.is_some_and(|time| entry.modified <= time)
                || query.modified_before.is_some_and(|time| entry.modified >= time)
            {
                return false;
            }

            // Single extension filtering
            if query.extension.as_ref().is_some_and(|extension| {
                entry.path.extension().and_then(|value| value.to_str()) != Some(extension.as_str())
            }) {
                return false;
            }

            // Multiple extensions filtering (OR logic)
            if !query.extensions.is_empty() {
                let has_valid_extension = entry
                    .path
                    .extension()
                    .and_then(|value| value.to_str())
                    .is_some_and(|ext| query.extensions.iter().any(|e| e == ext));
                if !has_valid_extension {
                    return false;
                }
            }

            // File type filtering
            if query.file_type.is_some_and(|file_type| entry.file_type != file_type) {
                return false;
            }

            // Hidden files filtering
            if !query.hidden
                && let Some(name) = entry.name()
                && name.starts_with('.')
            {
                return false;
            }

            // Pattern matching
            if query.pattern.is_empty() {
                return true;
            }

            let relative = entry.relative_path.to_string_lossy();
            let name = entry.name().unwrap_or_default();
            let search_target = if query.full_path { relative.as_ref() } else { name };

            match query.kind {
                MatchKind::Exact => {
                    if query.fixed_strings {
                        search_target.contains(&query.pattern)
                    } else {
                        // Case-insensitive exact match
                        let pattern = if query.case_sensitive.unwrap_or(false) {
                            query.pattern.clone()
                        } else {
                            query.pattern.to_lowercase()
                        };
                        let target = if query.case_sensitive.unwrap_or(false) {
                            search_target.to_string()
                        } else {
                            search_target.to_lowercase()
                        };
                        target.contains(&pattern)
                    }
                }
                MatchKind::Glob => {
                    let ignore_case = query
                        .case_sensitive
                        .map_or_else(|| query.pattern.to_lowercase() == query.pattern, |cs| !cs);
                    glob_match_case(&query.pattern, search_target, ignore_case)
                }
                MatchKind::Regex => {
                    let ignore_case = query
                        .case_sensitive
                        .map_or_else(|| query.pattern.to_lowercase() == query.pattern, |cs| !cs);
                    regex.as_ref().is_some_and(|re| {
                        if ignore_case {
                            re.is_match(search_target)
                        } else {
                            re.captures(search_target).is_some()
                        }
                    })
                }
            }
        })
        .cloned()
        .collect()
}

fn glob_match(pattern: &str, value: &str) -> bool {
    globset::Glob::new(pattern).is_ok_and(|glob| glob.compile_matcher().is_match(value))
}

fn glob_match_case(pattern: &str, value: &str, ignore_case: bool) -> bool {
    if ignore_case {
        let pattern_lower = pattern.to_lowercase();
        let value_lower = value.to_lowercase();
        glob_match(&pattern_lower, &value_lower)
    } else {
        glob_match(pattern, value)
    }
}

/// Kept as a small helper for callers that need to calculate depth.
#[must_use]
pub fn path_depth(path: &Path) -> usize {
    path.components().count()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::UNIX_EPOCH;

    #[test]
    fn test_parse_size_constraints() {
        let empty: Vec<String> = vec![];
        let (min1, max1) = FindQuery::parse_size_constraints(&empty).unwrap();
        assert_eq!(min1, None);
        assert_eq!(max1, None);

        let (min2, max2) = FindQuery::parse_size_constraints(&["5GB".to_string()]).unwrap();
        assert_eq!(min2, Some(5_000_000_000));
        assert_eq!(max2, Some(5_000_000_000));

        let (min3, max3) = FindQuery::parse_size_constraints(&["-5GB".to_string()]).unwrap();
        assert_eq!(min3, None);
        assert_eq!(max3, Some(4_999_999_999));
    }

    #[test]
    fn test_filter_entries() {
        let entries = vec![
            FileEntry {
                path: PathBuf::from("/test/file1.txt"),
                relative_path: PathBuf::from("file1.txt"),
                size: 100,
                modified: UNIX_EPOCH,
                hash: blake3::hash(b""),
                file_type: FileType::File,
            },
            FileEntry {
                path: PathBuf::from("/test/.hidden"),
                relative_path: PathBuf::from(".hidden"),
                size: 50,
                modified: UNIX_EPOCH,
                hash: blake3::hash(b""),
                file_type: FileType::File,
            },
            FileEntry {
                path: PathBuf::from("/test/subdir/file2.txt"),
                relative_path: PathBuf::from("subdir/file2.txt"),
                size: 200,
                modified: UNIX_EPOCH,
                hash: blake3::hash(b""),
                file_type: FileType::File,
            },
        ];

        // Test hidden files filtering
        let query1 = FindQuery {
            hidden: false,
            ..FindQuery::default()
        };
        let filtered1 = filter_entries(&entries, &query1);
        assert_eq!(filtered1.len(), 2);

        // Test hidden files shown
        let query2 = FindQuery {
            hidden: true,
            ..FindQuery::default()
        };
        let filtered2 = filter_entries(&entries, &query2);
        assert_eq!(filtered2.len(), 3);

        // Test depth filtering
        let query3 = FindQuery {
            max_depth: Some(1),
            hidden: true,
            ..FindQuery::default()
        };
        let filtered3 = filter_entries(&entries, &query3);
        assert_eq!(filtered3.len(), 2);

        // Test size filtering
        let query4 = FindQuery {
            min_size: Some(150),
            hidden: true,
            ..FindQuery::default()
        };
        let filtered4 = filter_entries(&entries, &query4);
        assert_eq!(filtered4.len(), 1);
        assert_eq!(filtered4.first().unwrap().path, PathBuf::from("/test/subdir/file2.txt"));
    }
}