everywhere 0.1.1

A high-level, ergonomic Rust wrapper around the Everything SDK for fast Windows file search
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
469
470
471
472
#![cfg_attr(doc, doc = include_str!("../README.md"))]

use std::ffi::OsString;
use std::ops::Bound;
use std::ops::RangeBounds;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use std::time::SystemTime;

use everything_sdk::*;

/// Creates a new search with the given pattern using the Everything search
/// syntax.
///
/// This syntax supports wildcards (`*`, `?`), operators (`AND`, `OR`, `NOT`,
/// etc.) and a bunch of other features.
///
/// See [Searching - voidtools](https://www.voidtools.com/support/everything/searching/) for more details on the Everything search syntax.
pub fn search<S: Into<OsString>>(pattern: S) -> Search {
    Search {
        pattern: pattern.into(),
        ..Default::default()
    }
}

/// Creates a new search with the given regular expression pattern.
pub fn search_regex<S: Into<OsString>>(pattern: S) -> Search {
    Search {
        pattern: pattern.into(),
        regex: true,
        ..Default::default()
    }
}

/// Represents a search query to be executed against the Everything index.
///
/// Call [`Search::query_all`] or [`Search::query_range`] to execute the search
/// and retrieve results.
#[expect(clippy::struct_excessive_bools, reason = "Booleans are appropriate for configuring search options.")]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Default)]
pub struct Search {
    /// The search pattern to use.
    pub pattern: OsString,

    /// Whether to use regular expressions for the search pattern. `false`
    /// by default.
    ///
    /// If `false`, the Everything search syntax is used. This syntax supports
    /// wildcards (`*`, `?`), operators (`AND`, `OR`, `NOT`, etc.) and a bunch
    /// of other features.
    ///
    /// See [Searching - voidtools](https://www.voidtools.com/support/everything/searching/) for more details on the Everything search syntax.
    pub regex: bool,

    /// Specifies whether the search is case-sensitive. `false` by default.
    pub match_case: bool,

    /// Specifies whether full path matching is enabled. `false` by default.
    pub match_path: bool,

    /// Specifies whether the search matches whole words only. `false` by default.
    pub match_whole_word: bool,

    /// Specifies how search results are sorted. See [`SortKey`] for details.
    ///
    /// The default sort key is [`SortKey::Name`].
    pub sort_key: SortKey,

    /// Specifies how search results are sorted. See [`SortOrder`] for details.
    ///
    /// The default sort order is [`SortOrder::Ascending`].
    pub sort_order: SortOrder,

    /// Specifies additional file system metadata to include in search results.
    /// See [`ItemMetadata`] for details.
    ///
    /// By default, no additional metadata is included.
    pub requested_metadata: ItemMetadata,
}

/// Specifies the order in which search results are sorted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Default)]
pub enum SortOrder {
    #[default] Ascending,
    Descending,
}

/// Specifies the key by which search results are sorted.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Default)]
pub enum SortKey {
    #[default] Name,
    TypeName,
    Path,
    Size,
    Extension,
    DateCreated,
    DateModified,
    DateAccessed,
    Attributes,
}

/// Builder methods for [`Search`].
impl Search {
    /// Sets whether the search is case-sensitive.
    ///
    /// By default, searches are case-insensitive.
    #[must_use]
    #[inline]
    pub const fn match_case(mut self, case: bool) -> Self {
        self.match_case = case;
        self
    }

    /// Sets whether full path matching is enabled.
    ///
    /// By default, full path matching is disabled.
    #[must_use]
    #[inline]
    pub const fn match_path(mut self, path: bool) -> Self {
        self.match_path = path;
        self
    }

    /// Sets whether the search matches whole words only.
    ///
    /// By default, the search matches partial words.
    #[must_use]
    #[inline]
    pub const fn match_whole_word(mut self, whole_word: bool) -> Self {
        self.match_whole_word = whole_word;
        self
    }

    /// Sets the sort key and order for the search results.
    ///
    /// By default, results are sorted by name in ascending order.
    #[must_use]
    #[inline]
    pub const fn sort_by(mut self, key: SortKey, order: SortOrder) -> Self {
        self.sort_key = key;
        self.sort_order = order;
        self
    }

    /// Requests additional file system metadata to be included in search results.
    /// This method can be called multiple times and the requested metadata will
    /// be combined.
    ///
    /// By default, no additional metadata is included.
    ///
    /// See [`ItemMetadata`] for details.
    #[must_use]
    #[inline]
    pub fn request_metadata(mut self, metadata: ItemMetadata) -> Self {
        self.requested_metadata |= metadata;
        self
    }
}

/// Represents information about a file, folder or volume in the file system.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Item {
    /// The full path of the item.
    pub path: PathBuf,

    /// The type of the item.
    pub item_type: ItemType,

    /// The size of the item in bytes if available.
    /// `None` if the field was not requested via [`Search::request_metadata`]
    /// or is not available. In the latter case, the error is logged.
    pub size: Option<u64>,

    /// The creation date of the item if available.
    /// `None` if the field was not requested via [`Search::request_metadata`]
    /// or is not available. In the latter case, the error is logged.
    pub date_created: Option<SystemTime>,

    /// The modification date of the item if available.
    /// `None` if the field was not requested via [`Search::request_metadata`]
    /// or is not available. In the latter case, the error is logged.
    pub date_modified: Option<SystemTime>,

    /// The access date of the item if available.
    /// `None` if the field was not requested via [`Search::request_metadata`]
    /// or is not available. In the latter case, the error is logged.
    pub date_accessed: Option<SystemTime>,

    /// The attributes of the item if available.
    /// `None` if the field was not requested via [`Search::request_metadata`]
    /// or is not available. In the latter case, the error is logged.
    pub attributes: Option<u32>,
}

/// Represents the type of the [`Item`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ItemType {
    File,
    Folder,
    Volume,
}

bitflags::bitflags! {
    /// Specifies additional file system metadata to include in search results.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    #[derive(Default)]
    pub struct ItemMetadata: u32 {
        const SIZE =
            RequestFlags::EVERYTHING_REQUEST_SIZE.bits();
        const DATE_CREATED =
            RequestFlags::EVERYTHING_REQUEST_DATE_CREATED.bits();
        const DATE_MODIFIED =
            RequestFlags::EVERYTHING_REQUEST_DATE_MODIFIED.bits();
        const DATE_ACCESSED =
            RequestFlags::EVERYTHING_REQUEST_DATE_ACCESSED.bits();
        const ATTRIBUTES =
            RequestFlags::EVERYTHING_REQUEST_ATTRIBUTES.bits();
    }
}

/// Methods for executing the search.
impl Search {
    /// Executes the search and returns all matching items.
    ///
    /// This method returns all results without limit. For queries that may
    /// return a large number of results, consider using [`Self::query_range`]
    /// to limit results and avoid excessive memory usage.
    ///
    /// This method blocks until all results are retrieved. To avoid blocking,
    /// consider spawning a separate thread for the search.
    ///
    /// This method is equivalent to calling `query_range(..)`.
    #[must_use]
    #[inline]
    pub fn query_all(&self) -> Vec<Item> {
        self.query_range(..)
    }

    /// Executes the search and returns matching items within the specified
    /// range.
    ///
    /// Use this method to limit the number of results returned. For example,
    /// `query_range(..100)` returns the first 100 results, and
    /// `query_range(100..200)` returns results 100 through 199.
    ///
    /// For queries that may return a large number of results, it is strongly
    /// recommended to limit results to avoid excessive memory usage.
    ///
    /// This method blocks until the specified range of results is retrieved.
    /// To avoid blocking, consider spawning a separate thread for the search.
    ///
    /// # Caveats
    ///
    /// The Everything index is live and may change between queries. Consecutive
    /// calls (e.g., `query_range(0..100)` then `query_range(100..200)`) are not
    /// guaranteed to be consistent — files may be added, removed, or reordered
    /// between calls, causing gaps or overlaps. This limitation is inherent to
    /// the Everything indexing system. To get consistent results, fetch everything
    /// you need in a single call.
    #[must_use]
    pub fn query_range<R: RangeBounds<usize>>(&self, range: R) -> Vec<Item> {
        let range_start = match range.start_bound() {
            Bound::Included(&start) => start as u32,
            Bound::Excluded(&start) => start as u32 + 1,
            Bound::Unbounded => 0,
        };

        let range_len = match range.end_bound() {
            Bound::Included(&end) => end as u32 - range_start + 1,
            Bound::Excluded(&end) => end as u32 - range_start,
            Bound::Unbounded => u32::MAX,
        };

        let mut everything = everything_sdk::global().lock().unwrap();
        let mut searcher = everything.searcher();
        self.apply(&mut searcher);
        let result =
            searcher
                .set_offset(range_start)
                .set_max(range_len)
                .query();
        (0..result.num())
            .filter_map(|i| Item::from_result(self, &result, i))
            .collect()
    }

    fn apply(&self, searcher: &mut EverythingSearcher) {
        searcher
            .set_search(&self.pattern)
            .set_regex(self.regex)
            .set_match_case(self.match_case)
            .set_match_path(self.match_path)
            .set_match_whole_word(self.match_whole_word)
            .set_sort(
                convert_sort_type(
                    self.sort_key,
                    self.sort_order))
            .set_request_flags(
                RequestFlags::from_bits_truncate(
                    self.requested_metadata.bits()));
    }
}

impl Item {
    fn from_result(
        search: &Search,
        result: &EverythingResults,
        index: u32)
     -> Option<Self> {
        let item = result.at(index)?;

        let path =
            item.full_path_name(None).inspect_err(|err| {
                log::error!(concat!(
                    "Unable to retrieve the full path name of an item. ",
                    "Caused by the following error in the Everything SDK: {}"),
                    err);
            }).ok()?;

        let item_type_candidates = [
            item.is_file()
                .then_some(ItemType::File),
            item.is_folder()
                .then_some(ItemType::Folder),
            item.is_volume()
                .then_some(ItemType::Volume),
        ];

        debug_assert_eq!(
            item_type_candidates.iter().flatten().count(),
            1,
            concat!(
                "Encountering an item that is not exactly one of: file, folder, or volume. ",
                "This is likely a bug in the Everything SDK."));
        let item_type =
            item_type_candidates.iter().flatten().next().copied()?;

        Some(Self {
            path: path.clone(),
            item_type,
            size:
                get_metadata_from_item(
                    search,
                    &item,
                    &path,
                    ItemMetadata::SIZE,
                    EverythingItem::size),
            date_created:
                get_metadata_from_item(
                    search,
                    &item,
                    &path,
                    ItemMetadata::DATE_CREATED,
                    EverythingItem::date_created).map(convert_filetime),
            date_modified:
                get_metadata_from_item(
                    search,
                    &item,
                    &path,
                    ItemMetadata::DATE_MODIFIED,
                    EverythingItem::date_modified).map(convert_filetime),
            date_accessed:
                get_metadata_from_item(
                    search,
                    &item,
                    &path,
                    ItemMetadata::DATE_ACCESSED,
                    EverythingItem::date_accessed).map(convert_filetime),
            attributes:
                get_metadata_from_item(
                    search,
                    &item,
                    &path,
                    ItemMetadata::ATTRIBUTES,
                    EverythingItem::attributes),
        })
    }
}

/// Helper function to retrieve metadata from an [`EverythingItem`] if it was
/// requested in the [`Search`].
fn get_metadata_from_item<'a, T, F>(
    search: &Search,
    item: &'a EverythingItem<'a>,
    item_path: &Path,
    metadata_flag: ItemMetadata,
    metadata_getter: F)
    -> Option<T>
where
    F: FnOnce(&'a EverythingItem<'a>) -> Result<T>, {
    if search.requested_metadata.contains(metadata_flag) {
        match metadata_getter(item) {
            Ok(value) => Some(value),
            Err(err) => {
                log::error!(
                    concat!(
                        "Unable to retrieve requested metadata for {}. ",
                        "Caused by the following error in the Everything SDK: {}"),
                    item_path.display(),
                    err);
                None
            }
        }
    } else {
        None
    }
}

/// Converts a Windows FILETIME value to a [`SystemTime`].
///
/// FILETIME is the number of 100-nanosecond intervals since January 1, 1601.
fn convert_filetime(filetime: u64) -> SystemTime {
    // Difference between Windows epoch (1601-01-01) and Unix epoch (1970-01-01)
    // in 100-nanosecond intervals.
    const FILETIME_UNIX_DIFF: u64 = 116_444_736_000_000_000;

    let unix_100ns = filetime.saturating_sub(FILETIME_UNIX_DIFF);
    let secs = unix_100ns / 10_000_000;
    let nanos = ((unix_100ns % 10_000_000) * 100) as u32;
    SystemTime::UNIX_EPOCH + Duration::new(secs, nanos)
}

/// Combines the given [`SortKey`] and [`SortOrder`] into the corresponding
/// [`SortType`] used by [`everything_sdk`].
#[expect(clippy::enum_glob_use, reason = "Using glob imports improves readability in this match statement.")]
const fn convert_sort_type(key: SortKey, order: SortOrder) -> SortType {
    use SortKey::*;
    use SortType::*;
    use SortOrder::*;
    match (key, order) {
        (Name, Ascending) =>
            EVERYTHING_SORT_NAME_ASCENDING,
        (Name, Descending) =>
            EVERYTHING_SORT_NAME_DESCENDING,
        (TypeName, Ascending) =>
            EVERYTHING_SORT_TYPE_NAME_ASCENDING,
        (TypeName, Descending) =>
            EVERYTHING_SORT_TYPE_NAME_DESCENDING,
        (Path, Ascending) =>
            EVERYTHING_SORT_PATH_ASCENDING,
        (Path, Descending) =>
            EVERYTHING_SORT_PATH_DESCENDING,
        (Size, Ascending) =>
            EVERYTHING_SORT_SIZE_ASCENDING,
        (Size, Descending) =>
            EVERYTHING_SORT_SIZE_DESCENDING,
        (Extension, Ascending) =>
            EVERYTHING_SORT_EXTENSION_ASCENDING,
        (Extension, Descending) =>
            EVERYTHING_SORT_EXTENSION_DESCENDING,
        (DateCreated, Ascending) =>
            EVERYTHING_SORT_DATE_CREATED_ASCENDING,
        (DateCreated, Descending) =>
            EVERYTHING_SORT_DATE_CREATED_DESCENDING,
        (DateModified, Ascending) =>
            EVERYTHING_SORT_DATE_MODIFIED_ASCENDING,
        (DateModified, Descending) =>
            EVERYTHING_SORT_DATE_MODIFIED_DESCENDING,
        (DateAccessed, Ascending) =>
            EVERYTHING_SORT_DATE_ACCESSED_ASCENDING,
        (DateAccessed, Descending) =>
            EVERYTHING_SORT_DATE_ACCESSED_DESCENDING,
        (Attributes, Ascending) =>
            EVERYTHING_SORT_ATTRIBUTES_ASCENDING,
        (Attributes, Descending) =>
            EVERYTHING_SORT_ATTRIBUTES_DESCENDING,
    }
}