Add command line interface for testing queries

This commit is contained in:
Joakim Soderlund 2019-08-10 21:37:17 +00:00
parent 36f182620b
commit 6f1cd87f89
2 changed files with 53 additions and 4 deletions

View file

@ -101,4 +101,11 @@ where
pub fn iter(&self) -> impl Iterator<Item = &Story> {
self.index.iter()
}
pub fn filter<F>(&self, function: &F) -> Vec<&Story>
where
F: Sync + Fn(&Story) -> bool,
{
self.index.iter().filter(|s| function(s)).collect()
}
}

View file

@ -5,10 +5,14 @@ pub mod error;
pub mod query;
use std::env::args;
use std::io::stdin;
use std::io::stdout;
use std::io::Write;
use std::time::Instant;
use crate::archive::Fetcher;
use crate::error::Error;
use crate::query::parse;
fn exit(error: Error) -> ! {
eprintln!("{}", error);
@ -16,6 +20,16 @@ fn exit(error: Error) -> ! {
std::process::exit(1)
}
fn input() -> String {
let mut buffer = String::new();
print!(">>> ");
stdout().flush().unwrap();
stdin().read_line(&mut buffer).unwrap();
buffer
}
fn main() {
let argv = args().collect::<Vec<String>>();
@ -28,10 +42,38 @@ fn main() {
let start = Instant::now();
let result = Fetcher::from(&argv[1]);
let finish = Instant::now() - start;
let fetcher = result.map_err(exit).unwrap();
let finish = (Instant::now() - start).as_millis();
let count = fetcher.iter().count();
println!("Finished loading in {} milliseconds.", finish.as_millis());
println!("The archive contains {} stories.", fetcher.iter().count());
println!("Finished loading in {} milliseconds.", finish);
println!("The archive contains {} stories.", count);
loop {
let filter = match parse(&input()) {
Ok(filter) => filter,
Err(error) => {
println!("{}", error);
continue;
}
};
let start = Instant::now();
let stories = fetcher.filter(&filter);
let finish = (Instant::now() - start).as_millis();
let count = stories.len();
println!("Found {} stories in {} milliseconds!", count, finish);
if count > 32 {
continue;
}
for story in stories.iter() {
let key = &story.id;
let title = &story.title;
println!("[{}] {}", key, title);
}
}
}