use std::path::PathBuf;
use clap::Subcommand;
use blackline_xlsx::{CreateSpec, EditOp, EditOptions, Xlsx};
use crate::util::{atomic_write, print_json, read_json_arg, resolve_output, slice_range};
#[derive(Subcommand)]
pub enum XlsxCmd {
View {
file: PathBuf,
#[arg(long)]
sheet: Option<String>,
#[arg(long)]
json: bool,
},
Info {
file: PathBuf,
},
Find {
file: PathBuf,
query: String,
#[arg(long, default_value_t = 50)]
limit: usize,
#[arg(long)]
json: bool,
},
Edit {
file: PathBuf,
#[arg(long)]
ops: String,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(long)]
in_place: bool,
#[arg(long)]
lenient: bool,
#[arg(long)]
dry_run: bool,
#[arg(long)]
json: bool,
},
Create {
#[arg(long)]
spec: String,
#[arg(short, long)]
output: PathBuf,
},
Check {
file: PathBuf,
},
Cat {
file: PathBuf,
part: Option<String>,
},
Parts {
file: PathBuf,
#[arg(long)]
json: bool,
},
}
pub fn run(cmd: XlsxCmd) -> Result<i32, String> {
match cmd {
XlsxCmd::View { file, sheet, json } => {
let wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
let views = wb.view(sheet.as_deref()).map_err(|e| e.to_string())?;
if json {
print_json(&views)?;
} else {
for v in views {
println!("# {}", v.sheet);
for row in v.rows {
println!("{row}");
}
}
}
Ok(0)
}
XlsxCmd::Info { file } => {
let wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
print_json(&wb.info().map_err(|e| e.to_string())?)?;
Ok(0)
}
XlsxCmd::Find {
file,
query,
limit,
json,
} => {
let wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
let hits = wb.find(&query, limit).map_err(|e| e.to_string())?;
if json {
print_json(&hits)?;
} else {
for h in hits {
println!("{}!{}\t{}", h.sheet, h.cell, h.text);
}
}
Ok(0)
}
XlsxCmd::Edit {
file,
ops,
output,
in_place,
lenient,
dry_run,
json,
} => {
let ops: Vec<EditOp> = serde_json::from_str(&read_json_arg(&ops)?)
.map_err(|e| format!("usage: invalid ops JSON: {e}"))?;
let mut wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
let opts = EditOptions { lenient, dry_run };
let report = wb.edit(&ops, &opts).map_err(|e| e.to_string())?;
if json {
print_json(&report)?;
} else {
println!("{} applied, {} failed", report.applied, report.failed);
}
if !dry_run {
let (out, inplace) = resolve_output(&file, output.as_deref(), in_place)?;
if inplace {
atomic_write(&out, |tmp| wb.save(tmp).map_err(|e| e.to_string()))?;
} else {
wb.save(&out).map_err(|e| e.to_string())?;
}
}
Ok(if report.failed > 0 && !lenient { 1 } else { 0 })
}
XlsxCmd::Create { spec, output } => {
let spec: CreateSpec = serde_json::from_str(&read_json_arg(&spec)?)
.map_err(|e| format!("usage: invalid spec: {e}"))?;
Xlsx::create(&spec)
.map_err(|e| e.to_string())?
.save(&output)
.map_err(|e| e.to_string())?;
println!("wrote {}", output.display());
Ok(0)
}
XlsxCmd::Check { file } => {
let wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
let r = wb.check().map_err(|e| e.to_string())?;
print_json(&r)?;
Ok(if r.passed() { 0 } else { 1 })
}
XlsxCmd::Cat { file, part } => {
let wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
let part = part.unwrap_or_else(|| "xl/workbook.xml".into());
let xml = wb.package().part_xml(&part).map_err(|e| e.to_string())?;
print!("{}", String::from_utf8_lossy(&xml.to_pretty_bytes(" ")));
Ok(0)
}
XlsxCmd::Parts { file, json } => {
let wb = Xlsx::open(&file).map_err(|e| e.to_string())?;
let names = wb.package().part_names();
if json {
print_json(&names)?;
} else {
for n in names {
println!("{n}");
}
}
Ok(0)
}
}
}
#[allow(dead_code)]
fn _slice<T>(x: &[T]) -> &[T] {
slice_range(x, None, None)
}