grepdef 3.5.0

Quick search for symbol definitions in various programming languages
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#![warn(missing_docs)]
//! Quick search for symbol definitions in various programming languages
//!
//! Currently this supports Rust, JS (or TypeScript), Python, PHP, and Ruby.
//!
//! This can be used like "Go to definition" in an IDE, except that instead of using a language
//! server, it just searches for the definition using text parsing. This is less accurate but often
//! faster in projects with lots of files or where a language server won't work or hasn't yet
//! started.
//!
//! grepdef since 3.0 is written in Rust and is designed to be extremely fast.
//!
//! This can also be used as a library crate for other Rust programs.
//!
//! # Example
//!
//! The syntax of the CLI is similar to that of `grep` or `ripgrep`: first put the symbol you want
//! to search for (eg: a function name, class name, etc.) and then list the file(s) or directories
//! over which you want to search.
//!
//! ```text
//! $ grepdef parseQuery ./src
//! ./src/queries.js:function parseQuery {
//! ```
//!
//! Just like `grep`, you can add the `-n` option to include line numbers.
//!
//! ```text
//! $ grepdef -n parseQuery ./src
//! ./src/queries.js:17:function parseQuery {
//! ```
//!
//! The search will be faster if you specify what type of file you are searching for using the
//! `--type` option.
//!
//! ```text
//! $ grepdef --type js -n parseQuery ./src
//! ./src/queries.js:17:function parseQuery {
//! ```
//!
//! To use the crate from other Rust code, use [Searcher].
//!
//! ```
//! use grepdef::{Args, Searcher};
//!
//! for result in Searcher::new(Args::from_query("parseQuery")).unwrap().search().unwrap() {
//!     println!("{}", result.to_grep());
//! }
//! ```

use clap::Parser;
use colored::Colorize;
use ignore::Walk;
use regex::Regex;
use serde::Serialize;
use std::error::Error;
use std::fs;
use std::io::{self, BufRead, Seek};
use std::num::NonZero;
use std::sync::{mpsc, Arc};
use std::time;
use strum_macros::Display;
use strum_macros::EnumString;

mod file_type;
mod query_regex;
mod threads;

/// The command-line arguments to be used by [Searcher]
///
/// Can be passed to [Searcher::new].
///
/// The only required property is [Args::query].
///
/// # Example
///
/// ```
/// use grepdef::Args;
/// let config = Args::from_query("parseQuery");
/// assert_eq!(config.query, String::from("parseQuery"));
/// assert_eq!(config.file_path, None); // The current directory
/// assert_eq!(config.file_type, None); // Auto-detect the file type
/// assert_eq!(config.line_number, false); // Do not print line numbers
/// ```
#[derive(Parser, Debug, Default)]
#[command(
    version,
    arg_required_else_help = true,
    about = "Quick search for symbol definitions in various programming languages",
    long_about = "Quick search for symbol definitions in various programming languages"
)]
pub struct Args {
    /// (Required) The symbol name (function, class, etc.) to search for
    pub query: String,

    /// The file path(s) to search; recursively searches directories and respects .gitignore
    pub file_path: Option<Vec<String>>,

    /// The file type to search (js, php, rs, ts, py, rb); will guess if not set
    #[arg(short = 't', long = "type")]
    pub file_type: Option<String>,

    /// Show line numbers of matches if set
    #[arg(short = 'n', long = "line-number")]
    pub line_number: bool,

    /// Control color output ("never", "always", "auto"); default "auto"
    #[arg(long = "color")]
    pub color: Option<String>,

    /// Disable color (also supports NO_COLOR env)
    #[arg(long = "no-color")]
    pub no_color: bool,

    /// Limit the number of results
    #[arg(short = 'l', long = "limit")]
    pub limit: Option<usize>,

    /// (Advanced) Print debugging information
    #[arg(long = "debug")]
    pub debug: bool,

    /// (Advanced) The searching method
    #[arg(long = "search-method")]
    pub search_method: Option<SearchMethod>,

    /// (Advanced) The number of threads to use
    #[arg(short = 'j', long = "threads")]
    pub threads: Option<NonZero<usize>>,

    /// The output format; defaults to 'grep'
    #[arg(long = "format")]
    pub format: Option<SearchResultFormat>,
}

impl Args {
    /// Create a new set of arguments for [Searcher] with the minimal configuration
    pub fn from_query(query: &str) -> Args {
        Args {
            query: query.into(),
            ..Args::default()
        }
    }

    /// Create a new set of arguments for [Searcher]
    pub fn new(
        query: String,
        file_type: Option<String>,
        file_path: Option<Vec<String>>,
        line_number: bool,
    ) -> Args {
        Args {
            query,
            file_type,
            file_path,
            line_number,
            ..Args::default()
        }
    }
}

/// (Advanced) The type of underlying search algorithm to use
///
/// In general, a pre-scan is a good idea to quickly skip files that don't have a match, which
/// should be most files. You shouldn't need to change this from the default.
#[derive(clap::ValueEnum, Clone, Default, Debug, EnumString, PartialEq, Display)]
pub enum SearchMethod {
    /// Pre-scan each file by reading fully into memory and using a Regex
    #[default]
    PrescanRegex,

    /// Pre-scan each file by reading bytes until the query is found using memmem
    PrescanMemmem,

    /// Don't pre-scan files.
    NoPrescan,
}

/// The configuration used by a [Searcher]
///
/// Created by passing [Args] to [Config::new].
#[derive(Clone, Debug)]
struct Config {
    /// The symbol name (function, class, etc.) being searched for
    query: String,

    /// The list of file paths to search, ignoring invisible or gitignored files
    file_paths: Vec<String>,

    /// The type of files to scan (JS or PHP or RS)
    file_type: FileType,

    /// Include line numbers in results if true
    line_number: bool,

    /// Output debugging info during search if true
    debug: bool,

    /// Limit the number of results
    limit: Option<usize>,

    /// Explicitly disable color output if true
    no_color: bool,

    /// Explicitly control color output ("never", "always", "auto")
    color: ColorOption,

    /// The [SearchMethod] to use
    search_method: SearchMethod,

    /// The number of threads to use for searching files
    num_threads: NonZero<usize>,

    /// The output format
    format: SearchResultFormat,
}

impl Config {
    /// Create a new Config using an [Args]
    pub fn new(args: Args) -> Result<Config, String> {
        if args.debug {
            let args_formatted = format!("Creating config with args {:?}", args);
            println!("{}", args_formatted.yellow());
        }
        let file_paths = match args.file_path {
            Some(file_path) => file_path,
            None => vec![".".into()],
        };
        let file_type = match args.file_type {
            Some(file_type_string) => FileType::from_string(file_type_string.as_str())?,
            None => FileType::from_file_paths(&file_paths)?,
        };
        let color = match args.color {
            Some(color_option_string) => ColorOption::from_string(color_option_string.as_str())?,
            None => ColorOption::AUTO,
        };

        let num_threads = match args.threads {
            Some(threads) => threads,
            None => NonZero::new(5).expect("Default number of threads was invalid"),
        };

        let config = Config {
            query: args.query,
            file_paths,
            file_type,
            line_number: args.line_number,
            debug: args.debug,
            no_color: args.no_color,
            color,
            search_method: args.search_method.unwrap_or_default(),
            limit: args.limit,
            num_threads,
            format: args.format.unwrap_or_default(),
        };
        debug(&config, format!("Created config {:?}", config).as_str());
        Ok(config)
    }
}

/// The supported file types to search
///
/// You can turn a string into a [FileType] using [FileType::from_string] which also supports
/// type aliases like `javascript`, `javascriptreact`, or `typescript.tsx`.
#[derive(Clone, Debug)]
pub enum FileType {
    /// The JS (or TS) file type
    JS,

    /// The PHP file type
    PHP,

    /// The Rust file type
    RS,

    /// The Python file type
    PY,

    /// The Ruby file type
    RB,
}

impl FileType {
    /// Turn a string into a [FileType]
    ///
    /// You can turn a string into a [FileType] using [FileType::from_string] which also supports
    /// type aliases like `javascript`, `javascriptreact`, or `typescript.tsx`.
    pub fn from_string(file_type_string: &str) -> Result<FileType, String> {
        match file_type_string {
            "js" => Ok(FileType::JS),
            "ts" => Ok(FileType::JS),
            "jsx" => Ok(FileType::JS),
            "tsx" => Ok(FileType::JS),
            "javascript" => Ok(FileType::JS),
            "javascript.jsx" => Ok(FileType::JS),
            "javascriptreact" => Ok(FileType::JS),
            "typescript" => Ok(FileType::JS),
            "typescript.tsx" => Ok(FileType::JS),
            "typescriptreact" => Ok(FileType::JS),
            "php" => Ok(FileType::PHP),
            "rs" => Ok(FileType::RS),
            "rust" => Ok(FileType::RS),
            "py" => Ok(FileType::PY),
            "python" => Ok(FileType::PY),
            "rb" => Ok(FileType::RB),
            "ruby" => Ok(FileType::RB),
            _ => Err(format!("Invalid file type '{}'", file_type_string)),
        }
    }

    /// Get the textual representation of a [FileType]
    pub fn to_string(&self) -> String {
        match self {
            Self::JS => String::from("js"),
            Self::PHP => String::from("php"),
            Self::RS => String::from("rs"),
            Self::PY => String::from("py"),
            Self::RB => String::from("rb"),
        }
    }

    /// Try to guess a [FileType] based on a list of file paths
    ///
    /// This can examine files or recursive directories and try to determine the [FileType] to
    /// search for. It will return the file type of the first file it finds that matches one of the
    /// file type patterns the crate supports.
    ///
    /// If a directory includes multiple supported file types, this could be incorrect, so it's
    /// more reliable (and faster) to specify a file type explicitly.
    pub fn from_file_paths(file_paths: &Vec<String>) -> Result<FileType, &'static str> {
        for file_path in file_paths {
            let guess = file_type::guess_file_type_from_file_path(file_path);
            if let Some(value) = guess {
                return Ok(value);
            }
        }
        Err("Unable to guess file type from file paths")
    }
}

/// The supported arguments to the color option
///
/// You can turn a string into a [ColorOption] using [ColorOption::from_string].
#[derive(Clone, Debug)]
pub enum ColorOption {
    /// Always colorize
    ALWAYS,

    /// Never colorize
    NEVER,

    /// Auto-detect colorize
    AUTO,
}

impl ColorOption {
    /// Convert string to ColorOption
    pub fn from_string(color_option_string: &str) -> Result<ColorOption, String> {
        match color_option_string {
            "always" => Ok(ColorOption::ALWAYS),
            "never" => Ok(ColorOption::NEVER),
            "auto" => Ok(ColorOption::AUTO),
            _ => Err(format!("Invalid color option '{}'", color_option_string)),
        }
    }
}

/// The output format of [SearchResult]
#[derive(clap::ValueEnum, Clone, Default, Debug, EnumString, PartialEq, Display, Copy)]
pub enum SearchResultFormat {
    /// grep-like output; colon-separated path, line number, and text
    #[default]
    Grep,

    /// JSON output; one document per match
    JsonPerMatch,

    /// JSON output; one array document with all results
    JsonList,
}

/// The type of a search event
///
/// Search events are a concept that really only exists for [SearchResultFormat::JsonList].
///
/// When that flag is not enabled, [SearchResult] will have an `event_type` property that is always
/// [SearchEventType::NONE] and serialized instances of [SearchResult] will exclude the property
/// entirely.
///
/// When [SearchResultFormat::JsonList] is enabled, the list of results will include a
/// [SearchEventResult] at the start and end of its list with [SearchEventType::START] and
/// [SearchEventType::END] respectively. Also, each [SearchResult] match will have the `event_type`
/// property [SearchEventType::MATCH].
///
/// Most search events are of type [SearchEventType::MATCH], but [SearchResultFormat::JsonList]
/// includes results for the start and end of a search as well.
#[derive(Debug, PartialEq, Clone, Serialize)]
pub enum SearchEventType {
    /// The start of a search. Only present in [SearchResultFormat::JsonList].
    START,

    /// The end of a search. Only present in [SearchResultFormat::JsonList].
    END,

    /// A match. Each result will use this in [SearchResultFormat::JsonList].
    MATCH,

    /// An empty type. Primarily used when [SearchResultFormat::JsonList] is not set.
    NONE,
}

impl SearchEventType {
    fn is_empty(&self) -> bool {
        match self {
            SearchEventType::NONE => true,
            _ => false,
        }
    }
}

/// A search event that is not a result but rather just an informative marker
///
/// This is required because the [SearchResultFormat::JsonList] format needs "start" and "end" markers.
#[derive(serde::Serialize)]
struct SearchEventResult {
    pub event_type: SearchEventType,
}

impl SearchEventResult {
    pub fn to_json_in_list(&self) -> String {
        match self.event_type {
            SearchEventType::START => serde_json::to_string(self).unwrap_or_default() + ",",
            SearchEventType::END => serde_json::to_string(self).unwrap_or_default(),
            SearchEventType::MATCH => serde_json::to_string(self).unwrap_or_default() + ",",
            SearchEventType::NONE => String::from(""),
        }
    }
}

/// A result from calling [Searcher::search] or [Searcher::search_and_format]
///
/// Note that `line_number` will be set only if [Args::line_number] is true when searching.
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct SearchResult {
    /// The event type. This will always be [SearchEventType::MATCH].
    #[serde(skip_serializing_if = "SearchEventType::is_empty")]
    pub event_type: SearchEventType,

    /// The path to the file containing the symbol definition
    pub file_path: String,

    /// The line number of the symbol definition in the file
    pub line_number: Option<usize>,

    /// The symbol definition line
    pub text: String,
}

impl SearchResult {
    /// Return a formatted string for output in the "grep" format
    ///
    /// That is, either `file path:text on line` or, if [Args::line_number] is true,
    /// `file path:line number:text on line`.
    ///
    /// # Example
    ///
    /// If [Args::line_number] is true,
    ///
    /// ```text
    /// ./src/queries.js:17:function parseQuery {
    /// ```
    pub fn to_grep(&self) -> String {
        match self.line_number {
            Some(line_number) => format!(
                "{}:{}:{}",
                self.file_path.magenta(),
                line_number.to_string().green(),
                self.text
            ),
            None => format!("{}:{}", self.file_path.magenta(), self.text),
        }
    }

    /// Return a formatted string for output in the [SearchResultFormat::JsonPerMatch] format
    pub fn to_json_per_match(&self) -> String {
        serde_json::to_string(self).unwrap_or_default()
    }

    /// Return a formatted string for output in the [SearchResultFormat::JsonList] format
    pub fn to_json_in_list(&self) -> String {
        serde_json::to_string(self).unwrap_or_default() + ","
    }
}

/// A struct that can perform a search
///
/// This is the main API of this crate.
///
/// # Example
///
/// ```
/// use grepdef::{Args, Searcher};
/// let searcher = Searcher::new(Args::new(
///     String::from("parseQuery"),
///     None,
///     None,
///     true
/// ))
/// .unwrap();
///
/// for result in searcher.search_and_format().unwrap() {
///     println!("{}", result);
/// }
///
/// searcher.search_and_format_callback(|line| println!("{}", line));
/// ```
pub struct Searcher {
    config: Config,
}

impl Searcher {
    /// Create a new Config using an [Args]
    pub fn new(args: Args) -> Result<Searcher, String> {
        let config = Config::new(args)?;
        Ok(Searcher { config })
    }

    /// Perform the search and return formatted strings
    pub fn search_and_format(&self) -> Result<Vec<String>, Box<dyn Error>> {
        let mut results: Vec<String> = vec![];
        let error = self.search_and_format_callback(|result| results.push(result));
        match error {
            Ok(_) => Ok(results),
            Err(err) => Err(err),
        }
    }

    /// Perform the search and run a callback for each formatted string
    pub fn search_and_format_callback<F>(&self, mut callback: F) -> Result<(), Box<dyn Error>>
    where
        F: FnMut(String),
    {
        if self.config.format == SearchResultFormat::JsonList {
            callback(String::from("["));
            let event = SearchEventResult {
                event_type: SearchEventType::START,
            };
            callback(event.to_json_in_list());
        }
        let error = self.search_callback(|result| match self.config.format {
            SearchResultFormat::Grep => callback(result.to_grep()),
            SearchResultFormat::JsonPerMatch => callback(result.to_json_per_match()),
            SearchResultFormat::JsonList => callback(result.to_json_in_list()),
        });
        if self.config.format == SearchResultFormat::JsonList {
            let event = SearchEventResult {
                event_type: SearchEventType::END,
            };
            callback(event.to_json_in_list());
            callback(String::from("]"));
        }
        error
    }

    /// Perform the search and call the callback for each result
    pub fn search_callback<F>(&self, mut callback: F) -> Result<(), Box<dyn Error>>
    where
        F: FnMut(SearchResult),
    {
        // Don't try to even calculate elapsed time if we are not going to print it
        let start: Option<time::Instant> = if self.config.debug {
            Some(time::Instant::now())
        } else {
            None
        };
        let re = query_regex::get_regex_for_query(&self.config.query, &self.config.file_type);
        let config = Arc::new(self.config.clone());
        let mut pool = threads::ThreadPool::new(config.num_threads, config.debug);

        match config.color {
            ColorOption::ALWAYS => colored::control::set_override(true),
            ColorOption::NEVER => colored::control::set_override(false),
            ColorOption::AUTO => (),
        }
        if config.no_color {
            colored::control::set_override(false);
        }

        self.debug("Starting searchers");
        let mut searched_file_count = 0;

        // Create a scope for tx to live in because it is cloned by all files and we need all
        // senders to go out of scope for the iterator to end.
        let rx = {
            let (tx, rx) = mpsc::channel();
            for file_path in &config.file_paths {
                for entry in Walk::new(file_path) {
                    let path = match entry {
                        Ok(path) => path.into_path(),
                        Err(err) => {
                            return Err(Box::new(err));
                        }
                    };
                    if path.is_dir() {
                        continue;
                    }
                    let path = match path.to_str() {
                        Some(p) => p.to_string(),
                        None => {
                            return Err(Box::from("Error getting string from path"));
                        }
                    };
                    if !file_type::path_matches_file_type(&path, &config.file_type) {
                        continue;
                    }
                    searched_file_count += 1;

                    let re1 = re.clone();
                    let path1 = path.clone();
                    let config1 = Arc::clone(&config);
                    let tx1 = tx.clone();
                    pool.execute(move || {
                        search_file(
                            &re1,
                            &path1,
                            &config1,
                            move |file_results: Vec<SearchResult>| {
                                // NOTE: it would be nice to have better error handling for if this
                                // message send fails, but since normal error handling would happen through
                                // message sending, I don't know what else to do.
                                let _ = tx1.send(file_results);
                            },
                        );
                    })
                }
            }
            rx
        };

        self.debug("Listening to searcher results");
        let mut result_counter: usize = 0;
        'all_results: for received_results in rx {
            for received_result in received_results {
                result_counter += 1;
                callback(received_result);
                // Don't try to even calculate elapsed time if we are not going to print it
                if let (true, Some(start)) = (self.config.debug, start) {
                    self.debug(
                        format!("Found a result in {} ms", start.elapsed().as_millis()).as_str(),
                    );
                }
                if let Some(i) = self.config.limit {
                    self.debug(format!("This is result {}; limit {}", result_counter, i).as_str());
                    if result_counter >= i {
                        self.debug("Limit reached");
                        pool.stop();
                        break 'all_results;
                    }
                }
            }
        }

        self.debug("Waiting for searchers to complete");
        pool.wait_for_all_jobs_and_stop();
        self.debug("Searchers complete");

        // Don't try to even calculate elapsed time if we are not going to print it
        if let (true, Some(start)) = (self.config.debug, start) {
            self.debug(
                format!(
                    "Scanned {} files in {} ms",
                    searched_file_count,
                    start.elapsed().as_millis()
                )
                .as_str(),
            );
        }
        Ok(())
    }

    /// Perform the search and return [SearchResult] structs
    pub fn search(&self) -> Result<Vec<SearchResult>, Box<dyn Error>> {
        let mut results: Vec<SearchResult> = vec![];
        let search_result = self.search_callback(|result| results.push(result));
        match search_result {
            Ok(_) => Ok(results),
            Err(err) => Err(err),
        }
    }

    fn debug(&self, output: &str) {
        if self.config.debug {
            println!("{}", output.yellow());
        }
    }
}

fn debug(config: &Config, output: &str) {
    if config.debug {
        println!("{}", output.yellow());
    }
}

fn search_file<F>(re: &Regex, file_path: &str, config: &Config, callback: F)
where
    F: FnOnce(Vec<SearchResult>) + Send + 'static,
{
    debug(config, format!("Scanning file {}", file_path).as_str());
    let file = fs::File::open(file_path);

    match file {
        Ok(mut file) => {
            // Scan the file in big chunks to see if it has what we are looking for. This is more efficient
            // than going line-by-line on every file since matches should be quite rare.
            if match config.search_method {
                SearchMethod::PrescanRegex => !file_type::does_file_match_regexp(&file, re),
                SearchMethod::PrescanMemmem => {
                    !file_type::does_file_match_query(&file, &config.query)
                }
                SearchMethod::NoPrescan => false,
            } {
                debug(
                    config,
                    format!("Presearch of {} found no match; skipping", &file_path).as_str(),
                );
                callback(vec![]);
                return;
            }

            let rewind_result = file.rewind();
            if rewind_result.is_err() {
                callback(vec![]);
                return;
            }
            debug(
                config,
                format!(
                    "Presearch of {} was successful; searching for line",
                    &file_path
                )
                .as_str(),
            );
            callback(search_file_line_by_line(re, file_path, &file, config));
        }
        Err(_) => {
            callback(vec![]);
        }
    }
}

fn search_file_line_by_line(
    re: &Regex,
    file_path: &str,
    file: &fs::File,
    config: &Config,
) -> Vec<SearchResult> {
    let lines = io::BufReader::new(file).lines();
    let mut line_counter = 0;

    lines
        .filter_map(|line| {
            line_counter += 1;
            if !match &line {
                Ok(line) => re.is_match(line),
                Err(_) => false,
            } {
                return None;
            }

            let text = match line {
                Ok(line) => line,
                // If reading the line causes an error (eg: invalid UTF), then skip it by treating
                // it as empty.
                Err(_err) => String::from(""),
            };

            Some(SearchResult {
                event_type: match config.format {
                    SearchResultFormat::JsonList => SearchEventType::MATCH,
                    _ => SearchEventType::NONE,
                },
                file_path: String::from(file_path),
                line_number: if config.line_number {
                    Some(line_counter)
                } else {
                    None
                },
                text: text.trim().into(),
            })
        })
        .collect()
}