TextSearcher

Struct TextSearcher 

Source
pub struct TextSearcher { /* private fields */ }
Expand description

Text searcher that uses ripgrep as a library for fast text searching.

§Rust Book Reference

Chapter 5.3: Method Syntax https://doc.rust-lang.org/book/ch05-03-method-syntax.html

Chapter 10.2: Traits as Parameters https://doc.rust-lang.org/book/ch10-02-traits.html

§Educational Notes - The Builder Pattern

This struct demonstrates the builder pattern, a common Rust idiom for constructing complex objects with many optional parameters.

Key characteristics:

  1. Private fields prevent direct construction
  2. new() provides sensible defaults
  3. Builder methods take mut self and return Self
  4. Final search() method takes &self (doesn’t consume)

Why this pattern?

  • Avoids constructors with many parameters
  • Makes optional configuration explicit
  • Enables method chaining for readability
  • Compile-time validation of configuration

Implementations§

Source§

impl TextSearcher

Source

pub fn new(base_dir: PathBuf) -> Self

Create a new TextSearcher with default settings.

§Rust Book Reference

Chapter 5.3: Method Syntax - Associated Functions https://doc.rust-lang.org/book/ch05-03-method-syntax.html#associated-functions

§Educational Notes - Builder Constructor

This is an associated function (not a method) that creates a new instance. It’s called with TextSearcher::new(...) rather than on an instance.

Design decisions:

  • Takes only required parameter (base_dir)
  • Sets sensible defaults for all optional fields
  • Returns owned Self (not &Self)

Usage pattern:

let searcher = TextSearcher::new(PathBuf::from("/path"))
    .case_sensitive(true)    // Optional: override default
    .respect_gitignore(false); // Optional: override default
Source

pub fn respect_gitignore(self, value: bool) -> Self

Set whether to respect .gitignore files (default: true).

§Rust Book Reference

Chapter 5.3: Method Syntax https://doc.rust-lang.org/book/ch05-03-method-syntax.html

§Educational Notes - Builder Method Pattern

This method demonstrates the builder pattern’s key technique:

pub fn respect_gitignore(mut self, value: bool) -> Self {
//                       ^^^^^^^^              ^^^^^^
//                       Takes ownership       Returns ownership
    self.respect_gitignore = value;
    self  // Return modified self for chaining
}

Why mut self instead of &mut self?

  • mut self takes ownership, allowing method chaining
  • &mut self would require explicit returns and be less ergonomic
  • Ownership transfer prevents using partially-configured builders

Method chaining:

TextSearcher::new(dir)
    .respect_gitignore(false)  // Consumes and returns Self
    .case_sensitive(true)      // Consumes and returns Self
    .search("text")            // Final method takes &self
Source

pub fn case_sensitive(self, value: bool) -> Self

Set whether search is case-sensitive (default: false).

§Educational Notes

Same builder pattern as respect_gitignore(). Each builder method:

  1. Takes ownership of self
  2. Modifies the field
  3. Returns ownership for chaining
Source

pub fn word_match(self, value: bool) -> Self

Set whether to match whole words only (default: false)

Source

pub fn is_regex(self, value: bool) -> Self

Set whether to treat the query as a regex (default: false)

Source

pub fn add_globs(self, globs: Vec<String>) -> Self

Add glob patterns to include

Source

pub fn add_exclusions(self, exclusions: Vec<String>) -> Self

Add exclusion patterns

Source

pub fn search(&self, text: &str) -> Result<Vec<Match>>

Search for text and return all matches.

§Rust Book Reference

Chapter 16.2: Message Passing with Channels https://doc.rust-lang.org/book/ch16-02-message-passing.html

Chapter 13.1: Closures https://doc.rust-lang.org/book/ch13-01-closures.html

§Educational Notes - Concurrent Search with Channels

This method demonstrates concurrent programming using message passing:

  1. Create channel: let (tx, rx) = mpsc::channel()
  2. Spawn workers: Each thread gets a cloned sender (tx.clone())
  3. Send results: Workers send matches through the channel
  4. Drop original sender: Critical for terminating the receiver
  5. Collect results: Main thread receives all matches

Why channels instead of shared state?

  • No locks needed (no Mutex)
  • Ownership prevents data races
  • Natural producer-consumer pattern
  • Rust’s type system ensures thread safety
§Arguments
  • text - The text to search for
§Returns

A vector of Match structs containing file path, line number, and content

Trait Implementations§

Source§

impl Default for TextSearcher

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.