use anyhow::Result;
use clap::Args;
use lineprior::load_prior_book;
use std::fs::File;
use std::path::PathBuf;
use std::process::ExitCode;
#[derive(Args)]
pub struct QueryArgs {
input: PathBuf,
#[arg(long)]
state: String,
#[arg(long)]
top_k: Option<usize>,
#[arg(long, value_delimiter = ',')]
recent_actions: Vec<String>,
}
pub fn run(args: QueryArgs) -> Result<ExitCode> {
let file = match File::open(&args.input) {
Ok(f) => f,
Err(err) => {
eprintln!("error: opening {}: {err}", args.input.display());
return Ok(ExitCode::from(3));
}
};
let book = match load_prior_book(file) {
Ok(book) => book,
Err(err) => {
eprintln!("error: {err}");
return Ok(super::exit_code_for_lineprior_error(&err));
}
};
if args.recent_actions.is_empty() {
for candidate in book.query(&args.state, args.top_k) {
println!("{}", serde_json::to_string(&candidate)?);
}
} else {
let result = book.query_with_context(&args.state, &args.recent_actions, args.top_k);
println!("{}", serde_json::to_string(&result)?);
}
Ok(ExitCode::from(0))
}