diff --git a/src/archive/fetcher.rs b/src/archive/fetcher.rs index 002d3e1..2550134 100644 --- a/src/archive/fetcher.rs +++ b/src/archive/fetcher.rs @@ -101,4 +101,11 @@ where pub fn iter(&self) -> impl Iterator { self.index.iter() } + + pub fn filter(&self, function: &F) -> Vec<&Story> + where + F: Sync + Fn(&Story) -> bool, + { + self.index.iter().filter(|s| function(s)).collect() + } } diff --git a/src/main.rs b/src/main.rs index 9f027d0..fbe4b0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::>(); @@ -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); + } + } }