Thanks for creating ripgrep and extracting libripgrep.
https://github.com/mrageh/simplegrep
I'm using libripgrep
Mac
I'm new to Rust and I'm trying to build a toy project that searches through directories and then instead of outputting the result to the terminal or returning JSON. It gives me back a simple list
that matches my certain criteria, where I can continue modifying it in Rust.
https://github.com/mrageh/simplegrep/blob/master/src/main.rs#L55
The SummaryBuilder returns the exact results I want but then when I call build on it and pass it the cli::stdout it prints to the terminal. Which is expected behavior.
But I'm wondering if I could pass something else to build like a vector and return a populated list from the SummaryBuilder.
@mrageh SummaryBuilder::build requires a WriteColor. You can't pass something else to it. Indeed, the entire point of the grep-printer crate is specifically to craft an output format that is readable by end users in a style they are familiar with. If you want to handle matches as structured data, then the grep-printer crate is the wrong tool for that.
You are on the right track with respect to "giving something else" to build. That is, this is the code you'll want to change: https://github.com/mrageh/simplegrep/blob/223bf40a3507785b90529180d0846367a08cba77/src/main.rs#L71-L75
In particular, that corresponds to Searcher::search_path, which a matcher, a file path to search and something that implements Sink. So all you need to do is create a new type and implement the Sink trait. Then pass a value of that type to search_path and get rid of grep_printer entirely.
Note that there are some pre-built types that implement Sink for you, which you might find convenient instead of defining your own types: https://docs.rs/grep-searcher/0.1.4/grep_searcher/sinks/index.html
There's a short example using the sinks::UTF8 sink here: https://docs.rs/grep-searcher/0.1.4/grep_searcher/index.html#example
@BurntSushi your comment here really saved my day!
Most helpful comment
@mrageh
SummaryBuilder::buildrequires aWriteColor. You can't pass something else to it. Indeed, the entire point of thegrep-printercrate is specifically to craft an output format that is readable by end users in a style they are familiar with. If you want to handle matches as structured data, then thegrep-printercrate is the wrong tool for that.You are on the right track with respect to "giving something else" to
build. That is, this is the code you'll want to change: https://github.com/mrageh/simplegrep/blob/223bf40a3507785b90529180d0846367a08cba77/src/main.rs#L71-L75In particular, that corresponds to
Searcher::search_path, which a matcher, a file path to search and something that implementsSink. So all you need to do is create a new type and implement theSinktrait. Then pass a value of that type tosearch_pathand get rid ofgrep_printerentirely.Note that there are some pre-built types that implement
Sinkfor you, which you might find convenient instead of defining your own types: https://docs.rs/grep-searcher/0.1.4/grep_searcher/sinks/index.htmlThere's a short example using the
sinks::UTF8sink here: https://docs.rs/grep-searcher/0.1.4/grep_searcher/index.html#example