use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use crate::transcoder::TargetEncoding;
#[derive(Debug, Parser)]
#[command(
name = "utf8-forge",
version,
about = "Idempotent charset transcoder for source trees (default target: UTF-8 without BOM)"
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Debug, Subcommand)]
pub enum Commands {
Scan(TreeArgs),
Convert(ConvertArgs),
Verify(TreeArgs),
Fix(FixArgs),
Diff(DiffArgs),
}
#[derive(Debug, Args, Clone)]
pub struct TreeArgs {
#[arg(short = 'r', long = "root", default_value = ".")]
pub root: PathBuf,
#[arg(short = 'e', long = "ext", value_delimiter = ',')]
pub ext: Vec<String>,
#[arg(short = 'x', long = "exclude", value_delimiter = ',')]
pub exclude: Vec<String>,
#[arg(short = 'X', long = "exclude-ext", value_delimiter = ',')]
pub exclude_ext: Vec<String>,
#[arg(long = "exclude-glob")]
pub exclude_glob: Vec<String>,
#[arg(long = "include-rc")]
pub include_rc: bool,
#[arg(long = "include-msbuild")]
pub include_msbuild: bool,
#[arg(long = "include-hidden")]
pub include_hidden: bool,
#[arg(long = "max-file-size")]
pub max_file_size: Option<u64>,
#[arg(long = "config")]
pub config: Option<PathBuf>,
#[arg(long = "ignore-file", conflicts_with = "no_ignore_file")]
pub ignore_file: Option<PathBuf>,
#[arg(long = "no-ignore-file")]
pub no_ignore_file: bool,
#[arg(long = "threads")]
pub threads: Option<usize>,
#[arg(long = "report")]
pub report: Option<PathBuf>,
#[arg(short = 'q', long = "quiet")]
pub quiet: bool,
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
}
impl Default for TreeArgs {
fn default() -> Self {
TreeArgs {
root: PathBuf::from("."),
ext: Vec::new(),
exclude: Vec::new(),
exclude_ext: Vec::new(),
exclude_glob: Vec::new(),
include_rc: false,
include_msbuild: false,
include_hidden: false,
max_file_size: Some(209_715_200),
config: None,
ignore_file: None,
no_ignore_file: false,
threads: None,
report: None,
quiet: false,
verbose: false,
}
}
}
#[derive(Debug, Args, Clone)]
pub struct ConvertArgs {
#[command(flatten)]
pub common: TreeArgs,
#[arg(short = 't', long = "target")]
pub target: Option<TargetEncoding>,
#[arg(long = "dry-run")]
pub dry_run: bool,
#[arg(long = "backup-dir")]
pub backup_dir: Option<PathBuf>,
#[arg(long = "url")]
pub url: Option<String>,
#[arg(long = "output")]
pub output: Option<PathBuf>,
}
impl Default for ConvertArgs {
fn default() -> Self {
ConvertArgs {
common: TreeArgs::default(),
target: Some(TargetEncoding::Utf8),
dry_run: false,
backup_dir: None,
url: None,
output: None,
}
}
}
impl TreeArgs {
pub fn effective_max_file_size(&self) -> u64 {
self.max_file_size.unwrap_or(209_715_200)
}
}
impl ConvertArgs {
pub fn target_or_default(&self) -> TargetEncoding {
self.target.unwrap_or(TargetEncoding::Utf8)
}
}
#[derive(Debug, Args, Clone)]
pub struct FixArgs {
#[command(flatten)]
pub common: TreeArgs,
#[arg(short = 't', long = "target")]
pub target: Option<TargetEncoding>,
#[arg(long = "dry-run")]
pub dry_run: bool,
#[arg(long = "backup-dir")]
pub backup_dir: Option<PathBuf>,
}
impl Default for FixArgs {
fn default() -> Self {
FixArgs {
common: TreeArgs::default(),
target: Some(TargetEncoding::Utf8),
dry_run: false,
backup_dir: None,
}
}
}
#[derive(Debug, Args, Clone)]
pub struct DiffArgs {
#[arg(long = "before")]
pub before: PathBuf,
#[arg(long = "after")]
pub after: PathBuf,
#[arg(long = "report")]
pub report: Option<PathBuf>,
#[arg(long = "fail-on-change")]
pub fail_on_change: bool,
}
impl From<Commands> for Cli {
fn from(command: Commands) -> Cli {
Cli { command }
}
}