use super::BuildConfigArgs;
use anyhow::{Context, Result};
use clap::Args;
use lineprior::{build_prior_book_from_reader, save_prior_book_with_config};
use std::fs::File;
use std::io::BufWriter;
use std::path::PathBuf;
use std::process::ExitCode;
#[derive(Args)]
pub struct BuildArgs {
input: PathBuf,
#[arg(long)]
out: PathBuf,
#[command(flatten)]
config: BuildConfigArgs,
#[arg(long)]
strict: bool,
}
pub fn run(args: BuildArgs) -> 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 config = args.config.into_build_config();
let output = match build_prior_book_from_reader(file, args.strict, &config) {
Ok(output) => output,
Err(err) => {
eprintln!("error: {err}");
return Ok(super::exit_code_for_lineprior_error(&err));
}
};
for warning in &output.warnings {
eprintln!("warning: {warning}");
}
if output.book.entries.is_empty() {
eprintln!("error: no usable data");
return Ok(ExitCode::from(2));
}
let stats = &output.stats;
eprintln!(
"stats: {}/{} observations kept, {}/{} candidates kept \
({} by min_count, {} by min_weighted_count, {} by min_confidence, {} by max_actions_per_state)",
stats.observations_kept,
stats.observations_kept + stats.observations_dropped_by_step_or_tag_filter,
stats.candidates_kept,
stats.candidates_before_filtering,
stats.candidates_dropped_by_min_count,
stats.candidates_dropped_by_min_weighted_count,
stats.candidates_dropped_by_min_confidence,
stats.candidates_dropped_by_max_actions_per_state,
);
let out_file =
File::create(&args.out).with_context(|| format!("creating {}", args.out.display()))?;
save_prior_book_with_config(&output.book, &config, BufWriter::new(out_file))
.context("writing prior book")?;
if !output.warnings.is_empty() {
return Ok(ExitCode::from(1));
}
Ok(ExitCode::from(0))
}