Skip to main content

Crate utf8_forge

Crate utf8_forge 

Source
Expand description

utf8-forge — a focused, idempotent, bidirectional charset transcoder for source trees (default output: UTF-8 without BOM).

This crate primarily ships the utf8-forge CLI; the library surface is public so integration tests and downstream pipelines can drive the same detection and transcoding machinery the binary uses. Detection is rule-based (BOM prefixes, then strict UTF-8, then strict GB18030) and never guesses; transcoding is strict on both ends, so mojibake is never written.

use utf8_forge::detector::detect;
use utf8_forge::transcoder::{TargetEncoding, transcode};

let bytes = [0xEF, 0xBB, 0xBF, b'h', b'i'];
assert_eq!(detect(&bytes).tag(), "utf-8-bom");

let converted = transcode(&bytes, TargetEncoding::Utf8).unwrap();
match converted {
    utf8_forge::transcoder::TranscodeOutcome::Changed(out) => assert_eq!(out, b"hi"),
    utf8_forge::transcoder::TranscodeOutcome::Unchanged => panic!("BOM must be stripped"),
}

§CLI orchestration

Per-file pipelines, report assembly, and the run() entry point with exit-code mapping:

  • 0 success
  • 1 non-fatal per-file issues (unknown, decode_error, encode_error, io_error, fetch_error)
  • 2 fatal (bad args, missing root, I/O setup failure) — also emitted by clap on parse errors
  • 3 verify/fix gate failure (non-UTF-8 whitelisted files; fix’s verify gate wins over convert-side issues)

Modules§

cli
clap definitions for the CLI surface.
config
TOML config file support (--config <PATH>).
detector
Encoding detection: a frozen rule-based algorithm, deliberately without statistical guessing so results are auditable and reproducible.
differ
Report-to-report comparison for the diff subcommand.
error
Error types shared across the transcoding pipeline.
fetcher
Optional remote download.
filter
File selection: extension whitelist + directory blacklist double gate.
ignorefile
Gitignore-style exclusion files (--ignore-file / auto-discovered <root>/.utf8forgeignore).
reporter
Console output + JSON reports.
transcoder
Strict decode / encode / transcode.
walker
Recursive candidate collection on top of walkdir.
writer
Crash-safe persistence.

Structs§

ConvertCtx
Shared context for tree-mode conversion.

Functions§

convert_file
Detect + transcode + (unless dry-run) backup + atomic write.
run
Dispatch a parsed CLI and map the outcome to a process exit code. Fatal setup errors print to stderr and yield exit code 2.
scan_file
Detect only. Oversized files are never read.
verify_file
Verify a file is UTF-8 without BOM. Unreadable / oversized files fail closed (a gate must not wave through what it cannot inspect). The size is re-checked after the read, mirroring the other processors, so a file that grows between stat and read cannot slip past the cap.