blob: d2cf5c18a277a7c6d75397a1617f268eb8b232e4 (
plain)
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
|
use std::{env, error::Error, io, process};
fn run() -> Result<(), Box<dyn Error>> {
// Get the query from the positional arguments.
// If one doesn't exist, return an error.
let query = match env::args().nth(1) {
None => return Err(From::from("expected 1 argument, but got none")),
Some(query) => query,
};
// Build CSV readers and writers to stdin and stdout, respectively.
let mut rdr = csv::Reader::from_reader(io::stdin());
let mut wtr = csv::Writer::from_writer(io::stdout());
// Before reading our data records, we should write the header record.
wtr.write_record(rdr.headers()?)?;
// Iterate over all the records in `rdr`, and write only records containing
// `query` to `wtr`.
for result in rdr.records() {
let record = result?;
if record.iter().any(|field| field == &query) {
wtr.write_record(&record)?;
}
}
// CSV writers use an internal buffer, so we should always flush when done.
wtr.flush()?;
Ok(())
}
fn main() {
if let Err(err) = run() {
println!("{}", err);
process::exit(1);
}
}
|