use crate::{IntSlice, IntSliceFormat};
use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;
use value_traits::slices::SliceByValue;
#[derive(Parser, Debug)]
#[command(name = "int", about = "Converts an integer sequence (e.g., permutations, eccentricities, components) between formats.", long_about = None, next_line_help = true)]
pub struct CliArgs {
pub src: PathBuf,
pub dst: PathBuf,
#[arg(long, value_enum, default_value_t)]
pub src_fmt: IntSliceFormat,
#[arg(long, value_enum, default_value_t)]
pub dst_fmt: IntSliceFormat,
}
fn store_slice(data: &[usize], dst: &PathBuf, dst_fmt: IntSliceFormat) -> Result<()> {
let max = data.iter().copied().max().unwrap_or(0);
dst_fmt.store(dst, data, Some(max))
}
fn collect_and_store(
slice: &impl SliceByValue<Value = usize>,
dst: &PathBuf,
dst_fmt: IntSliceFormat,
) -> Result<()> {
let data: Vec<usize> = (0..slice.len()).map(|i| slice.index_value(i)).collect();
let max = data.iter().copied().max().unwrap_or(0);
dst_fmt.store(dst, &data, Some(max))
}
pub fn main(args: CliArgs) -> Result<()> {
let loaded = args.src_fmt.load(&args.src)?;
log::info!(
"Loaded {} elements from {}",
loaded.len(),
args.src.display()
);
match &loaded {
IntSlice::Owned(v) => store_slice(v, &args.dst, args.dst_fmt)?,
#[cfg(target_pointer_width = "64")]
IntSlice::Java(j) => collect_and_store(j, &args.dst, args.dst_fmt)?,
IntSlice::Epserde(m) => store_slice(m.uncase(), &args.dst, args.dst_fmt)?,
IntSlice::BitFieldVec(m) => collect_and_store(m.uncase(), &args.dst, args.dst_fmt)?,
}
log::info!("Stored {} elements to {}", loaded.len(), args.dst.display());
Ok(())
}