mirror of
https://github.com/JockeTF/fimfareader.git
synced 2024-11-27 07:28:00 +01:00
Add command line interface for testing queries
This commit is contained in:
parent
36f182620b
commit
6f1cd87f89
2 changed files with 53 additions and 4 deletions
|
@ -101,4 +101,11 @@ where
|
||||||
pub fn iter(&self) -> impl Iterator<Item = &Story> {
|
pub fn iter(&self) -> impl Iterator<Item = &Story> {
|
||||||
self.index.iter()
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
50
src/main.rs
50
src/main.rs
|
@ -5,10 +5,14 @@ pub mod error;
|
||||||
pub mod query;
|
pub mod query;
|
||||||
|
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
|
use std::io::stdin;
|
||||||
|
use std::io::stdout;
|
||||||
|
use std::io::Write;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use crate::archive::Fetcher;
|
use crate::archive::Fetcher;
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
use crate::query::parse;
|
||||||
|
|
||||||
fn exit(error: Error) -> ! {
|
fn exit(error: Error) -> ! {
|
||||||
eprintln!("{}", error);
|
eprintln!("{}", error);
|
||||||
|
@ -16,6 +20,16 @@ fn exit(error: Error) -> ! {
|
||||||
std::process::exit(1)
|
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() {
|
fn main() {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
@ -28,10 +42,38 @@ fn main() {
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let result = Fetcher::from(&argv[1]);
|
let result = Fetcher::from(&argv[1]);
|
||||||
let finish = Instant::now() - start;
|
|
||||||
|
|
||||||
let fetcher = result.map_err(exit).unwrap();
|
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!("Finished loading in {} milliseconds.", finish);
|
||||||
println!("The archive contains {} stories.", fetcher.iter().count());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue