Skip to main content

cyto_ibu_cat/
lib.rs

1use anyhow::{Result, bail};
2
3use cyto_cli::ibu::ArgsCat;
4use cyto_io::match_output;
5use ibu::Writer;
6
7pub fn run(args: &ArgsCat) -> Result<()> {
8    // Build input handles
9    let mut inputs = args
10        .input
11        .inputs
12        .iter()
13        .map(|input| ibu::Reader::from_path(input))
14        .collect::<Result<Vec<_>, _>>()?;
15
16    // Validate all headers are the same
17    let mut og_header = None;
18    for reader in &mut inputs {
19        if let Some(og_header) = og_header {
20            if og_header != reader.header() {
21                bail!("IBU headers do not match!");
22            }
23        } else {
24            og_header = Some(reader.header());
25        }
26    }
27    let header = og_header.expect("No headers found... Something went wrong!");
28
29    // Build output handles
30    let output = match_output(args.output.as_ref())?;
31    let mut writer = Writer::new(output, header)?;
32
33    // Dump all records into the output
34    for reader in &mut inputs {
35        for record in reader {
36            writer.write_record(&record?)?;
37        }
38    }
39
40    // Flush the output
41    writer.finish()?;
42
43    Ok(())
44}