use std::path::PathBuf;
use std::process;
use clap::{Parser, Subcommand};
mod commands;
#[derive(Parser)]
#[command(name = "rdocx", version, about = "CLI tool for DOCX files")]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Inspect {
file: PathBuf,
#[arg(long)]
json: bool,
},
Text {
file: PathBuf,
},
Convert {
file: PathBuf,
#[arg(long, short = 't')]
to: String,
#[arg(long, short = 'o')]
output: Option<PathBuf>,
#[arg(long, default_value = "150")]
dpi: u32,
#[arg(long)]
font_dir: Option<PathBuf>,
#[arg(long)]
pages: Option<String>,
#[arg(long, default_value = "90")]
quality: u8,
#[arg(long)]
transparent: bool,
},
Diff {
file_a: PathBuf,
file_b: PathBuf,
},
Replace {
file: PathBuf,
#[arg(long, short = 'p')]
placeholder: String,
#[arg(long, short = 'v')]
value: String,
#[arg(long, short = 'o')]
output: PathBuf,
},
Validate {
file: PathBuf,
},
Render {
file: PathBuf,
#[arg(long, short = 'o')]
output_dir: Option<PathBuf>,
#[arg(long, default_value = "150")]
dpi: f64,
#[arg(long, conflicts_with = "pages")]
page: Option<usize>,
#[arg(long)]
pages: Option<String>,
#[arg(long, default_value = "png")]
format: String,
#[arg(long, default_value = "90")]
quality: u8,
#[arg(long)]
transparent: bool,
},
}
fn main() {
let cli = Cli::parse();
if let Command::Validate { file } = &cli.command {
match commands::validate(file) {
Ok(true) => return,
Ok(false) => process::exit(1),
Err(e) => {
eprintln!("Error: {e}");
process::exit(1);
}
}
}
let result = match cli.command {
Command::Inspect { file, json } => commands::inspect(&file, json),
Command::Text { file } => commands::text(&file),
Command::Convert {
file,
to,
output,
dpi,
font_dir,
pages,
quality,
transparent,
} => commands::convert(
&file,
&to,
output.as_deref(),
dpi,
font_dir.as_deref(),
commands::ImageOptions {
pages: pages.as_deref(),
quality,
transparent,
},
),
Command::Diff { file_a, file_b } => commands::diff(&file_a, &file_b),
Command::Replace {
file,
placeholder,
value,
output,
} => commands::replace(&file, &placeholder, &value, &output),
Command::Validate { .. } => unreachable!(),
Command::Render {
file,
output_dir,
dpi,
page,
pages,
format,
quality,
transparent,
} => commands::render(
&file,
output_dir.as_deref(),
dpi,
commands::RenderOptions {
page,
pages: pages.as_deref(),
format: &format,
quality,
transparent,
},
),
};
if let Err(e) = result {
eprintln!("Error: {e}");
process::exit(1);
}
}