utf8-forge 0.1.0

A focused, idempotent, bidirectional charset transcoder for source trees. Default output: UTF-8 without BOM.
Documentation
//! clap definitions for the CLI surface (`DESIGN.md §9`).
//!
//! All flags carry `///` doc comments in English (AGENT.md §5 policy).
//! clap parse failures exit with code 2, matching the documented fatal-code
//! contract; semantic conflicts are validated in [`TreeArgs::validate`].

use std::path::PathBuf;

use clap::{Args, Parser, Subcommand};

use crate::transcoder::TargetEncoding;

/// utf8-forge — a focused, idempotent, bidirectional charset transcoder.
#[derive(Debug, Parser)]
#[command(
    name = "utf8-forge",
    version,
    about = "Idempotent charset transcoder for source trees (default target: UTF-8 without BOM)"
)]
pub struct Cli {
    /// Selected subcommand.
    #[command(subcommand)]
    pub command: Commands,
}

/// Subcommands (`DESIGN.md §9`). Named `Commands` to avoid colliding with
/// the imported `clap::Subcommand` derive trait.
#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Detect only; report distribution (no writes).
    Scan(TreeArgs),
    /// Detect + transcode + atomic write (supports --dry-run, --url).
    Convert(ConvertArgs),
    /// Assert all whitelisted files are UTF-8 without BOM.
    Verify(TreeArgs),
}

/// Options shared by `scan` / `convert` / `verify` (tree mode).
#[derive(Debug, Args, Clone, Default)]
pub struct TreeArgs {
    /// Root directory to process recursively.
    #[arg(short = 'r', long = "root", default_value = ".")]
    pub root: PathBuf,

    /// Override the default extension whitelist (comma-separated).
    #[arg(short = 'e', long = "ext", value_delimiter = ',')]
    pub ext: Vec<String>,

    /// Extra excluded directory names (comma-separated, appended to defaults).
    #[arg(short = 'x', long = "exclude", value_delimiter = ',')]
    pub exclude: Vec<String>,

    /// Extra excluded extensions (comma-separated, appended).
    #[arg(short = 'X', long = "exclude-ext", value_delimiter = ',')]
    pub exclude_ext: Vec<String>,

    /// Extra glob pattern excluding root-relative paths (repeatable).
    #[arg(long = "exclude-glob")]
    pub exclude_glob: Vec<String>,

    /// Include .rc/.rc2/.dlg (rc.exe has limited UTF-8 support).
    #[arg(long = "include-rc")]
    pub include_rc: bool,

    /// Include .sln/.vcxproj/... (VS re-saves these with a BOM).
    #[arg(long = "include-msbuild")]
    pub include_msbuild: bool,

    /// Process dot-prefixed files and directories.
    #[arg(long = "include-hidden")]
    pub include_hidden: bool,

    /// Target encoding: utf-8 | utf-8-bom | gb18030 | utf-16le | utf-16be.
    #[arg(short = 't', long = "target", default_value = "utf-8")]
    pub target: TargetEncoding,

    /// Report what would change without writing anything.
    #[arg(long = "dry-run")]
    pub dry_run: bool,

    /// Mirror original bytes under this directory before overwriting.
    #[arg(long = "backup-dir")]
    pub backup_dir: Option<PathBuf>,

    /// Skip files above this size, recorded as too_large (bytes).
    #[arg(long = "max-file-size", default_value_t = 209_715_200)]
    pub max_file_size: u64,

    /// Rayon worker thread count (default: CPU count).
    #[arg(long = "threads")]
    pub threads: Option<usize>,

    /// Write a JSON report to this path.
    #[arg(long = "report")]
    pub report: Option<PathBuf>,

    /// Print errors only.
    #[arg(short = 'q', long = "quiet")]
    pub quiet: bool,

    /// Print one line per file.
    #[arg(short = 'v', long = "verbose")]
    pub verbose: bool,
}

/// `convert`-only additions.
#[derive(Debug, Args, Clone, Default)]
pub struct ConvertArgs {
    /// Shared tree-mode options (root ignored when --url is given).
    #[command(flatten)]
    pub common: TreeArgs,

    /// Fetch this remote file and transcode it (convert only).
    #[arg(long = "url")]
    pub url: Option<String>,

    /// Destination path for --url (derived from the URL when omitted).
    #[arg(long = "output")]
    pub output: Option<PathBuf>,
}