Locate: A Recursive Search Tool Written in Rust

A tool written in Rust to recursively search in all files located in a folder

PRAKHAR KAUSHIK
Better Programming

--

Photo by Evgeni Tcherkasski on Unsplash

How many times have you gone through files to find some string or origin of a function?

Locate is a tool written in Rust that goes through each file recursively and gives the exact location of the query string in the files.

Its blazingly fast, as it's written in Rust, and it can recursively walk the directories.

Motivation

I wanted to learn Rust, and I think the best way to learn a new language is by creating a project.

I have always used the search function in VS Code to find the origin of a function or the places where the function is used. If working with large projects, it sometimes becomes difficult to exactly locate all the points where a function is used. Of course, one can follow the imports to trace back to the origin of a function, but having a tool can be handy sometimes.

Note: As this is my first project in Rust, there’s a possibility that I have done certain things differently than I should have.

Let’s start.

Create a New Package

For starting a new package, we do cargo new, as this is a binary program:

cargo new locate --bin

This creates a folder structure:

. 
├── Cargo.toml
└── src
└── main.rs
1 directory, 2 files

Packages We Need

fstream = "0.1.2"
walkdir = "2.3.1"
argparse = "0.2.2"
colored = "1.9"
  • fstream will be used to read the file.
  • walkdir will be used to iterate over all the folders and files.
  • argparse is a simple argument parser to parse arguments.
  • colored will be used to colorize the terminal output.

Add these to your cargo.toml.

Now our cargo.toml will look something like this:

Code

Main code

Our basic workflow will be as follows. First, we’ll take path and query from the user. Then we need a function to iterate over all the folders in the path and check whether the query string is present in any file. If so, the path of the file will be passed to another function where the exact location of the query string will be located.

Walking the directory is easy, as we will be using walkdir for that.

Code for checking dir

We create a new Walkdir on the path given and apply a filter to silence the permission error.

Next, we check if the iter is a file. Then we use fstrem to check whether the query string is in the file or not.

Once we get the file having the query, it’s passed to another function, which locates the exact line number.

Code for checking file

It’s simple: We’re reading the whole file line by line. As it returns a string vector, we can easily iterate over each line and check if the query string is present or not.

If present, we simply print it. There is formatting for print (like .green(), .bold(), etc.) which is done with the help of colored crate we imported earlier.

That’s all. We have code for both parts, reading the file and finding the file.

What’s left is to parse arguments from the command line. The library argument parser is inspired by argparse in Python.

Code for argument parser

This part is also pretty simple. We’re creating two mutable variables (variables in Rust are immutable by default ). Then we’ll create a new argpaser object, set its description, and create our options. The best thing is that it automatically generates the help message using our description for each option.

Done. Let’s try it!

Type cargo build. You will find a binary created in target/debug. You can run it directly by cargo run or by using the binary you just created.

DEMO

You can find the whole source code at GitHub.

Also, if you don’t want to do all this, you can install it directly using: cargo install locate. Or you can check it out at cargo@locate.

Thanks for reading. Happy coding!

--

--

Python Developer interested in learning new languages, also an Active Open Source Contributor.