Expand description
recast core engine.
Compiles a regex pattern, walks a set of paths honoring ignore rules, produces per-file rewrites with unified-diff previews, enforces the match-count guard, and runs an idempotency (convergence) check before applying any change. The binary crate wires this engine to a CLI.
Modules§
- json
- JSON output schema for the recast CLI.
Structs§
- Apply
Outcome - Returned by
apply_changeson success: how many files were written and how many matches they covered. - Compiled
Pattern - A compiled regex paired with its replacement template. Construct with
CompiledPattern::compile; useCompiledPattern::is_convergentto check the idempotency invariant before scanning. - File
Change - One file’s worth of pending rewrite work.
afteris the full post-image used bycrate::apply_changes;diffis the already-rendered unified-diff string. The pre-image is dropped after the diff is built —apply_changesreads fromafter, not from the original on disk, so retaining the pre-image would just double the planner’s peak memory.permissionsis captured during the planner’s metadata read socrate::apply_changesdoesn’t have to issue a secondfs::metadatasyscall just to preserve the mode bits. - Pattern
Options - Knobs controlling how a pattern string is compiled into a regex.
- Plan
- Output of
plan_rewrite. Pass tocrate::apply_changesto commit. - Plan
Options - Knobs controlling a single
plan_rewriteinvocation. - Recovery
Summary - Summary of a
recover_sweepcall. - Rewrite
Outcome - Post-image of a single-file rewrite plus the match count that produced it. The pre-image stays with the caller — that’s why it isn’t carried here.
- Script
Rewriter - Pre-compiled Rhai script used as a per-match replacement callback.
- Search
File - Search
Match - Search
Options - Search
Plan - Structural
Outcome - Result of
structural_rewrite: the new source text plus the number of disjoint matches that were rewritten. - Walk
Options - Filters applied while walking
roots. - Workspace
Lock - RAII guard around an exclusively-locked lockfile. Drop to release.
Enums§
- Error
- Language
- Language registry for structural rewrites. Variants are gated by
the matching
lang-*cargo feature; build with--features lang-allto enable every grammar shipped today. - Plan
Outcome - Top-level result classification for a
Plan.
Functions§
- acquire_
workspace_ lock - Try to take an exclusive non-blocking lock on
lock_path. ReturnsError::Lockedimmediately if another process already holds it. - apply_
changes - Two-phase atomic commit.
- build_
pool - Build a rayon thread pool with the given thread count, or fall back to rayon’s default (one thread per logical CPU).
- check_
match_ counts - Enforce the
--at-least/--at-mostmatch-count guard. ReturnsError::TooFewMatches/Error::TooManyMatcheswhen the guard is violated; both variants map to theEXIT_GUARD_VIOLATED(2) exit code at the binary boundary. - compile_
friendly_ query - Compile a friendly pattern (target-language source with
$NAMEplaceholders) into a tree-sitter Query string. Exposed for callers that want to inspect or further manipulate the query. - label_
for_ path - Drop leading
./(and repeats thereof) from a path so unified-diff headers reada/src/a.rsinstead ofa/./src/a.rs. Absolute paths and plain relative paths pass through unchanged. - plan_
rewrite - Walk
roots, compilepattern, and produce aPlanof every file that would change whenreplacementis substituted. Honors the match-count guard, the convergence check, and the file/byte limits inopts. No filesystem writes happen here. - plan_
rewrite_ scripted - Like
plan_rewritebut each match drives a Rhai script callback instead of a static template. The pattern’sreplacementfield is ignored. - plan_
search - plan_
structural_ rewrite - Multi-file structural pipeline. Walks
roots, appliesstructural_rewriteper file, and folds the results into aPlanthat callers can pipe intocrate::apply_changes. Honorswalk_options,max_files,max_bytes, and theat_least/at_mostmatch-count guard fromopts. The convergence check and scripted-callback variants don’t apply here — structural rewrites aren’t re-probed against their own output. - plan_
structural_ search - Multi-file structural search. Walk
roots, run the query per file, fold intoSearchPlan. - recover_
sweep - Walk every regular file under
rootsand reconcile leftover.recast.bak.*/.recast.tmp.*siblings from a previous interrupted apply. - rewrite_
text - Apply
patterntobeforeand return the rewrite outcome. Counts matches and produces the new text in a single pass viaregex::replace_allwith anexpand-driven closure. - rewrite_
text_ scripted - Apply
patterntobefore, callingscriptonce per match. The script’s return value replaces each occurrence. Script errors abort the whole rewrite. - structural_
rewrite - Run a tree-sitter Query against
source, substitute captures intotemplateper match, and splice the resulting text into the source at each match’s replacement range. Overlapping match ranges are resolved greedy-first: the first match by start offset wins, later overlaps are skipped. - structural_
rewrite_ friendly - Friendlier counterpart to
structural_rewrite:pattern_sourceis written in the target language with$NAMEplaceholders. The pattern is compiled to a tree-sitter Query under the hood; the rewrite template uses the same$NAME/${NAME}substitution as the raw API. - structural_
search - Run a tree-sitter query against
sourceand return match locations without rewriting. - unified_
diff - Render a unified diff between
beforeandafterwith three lines of context, usinglabelfor thea/+b/header paths. - walk_
paths - Enumerate every regular file under
roots(sorted, dedup’d by the ignore crate) honoringopts. Directories, symlinks (unlessfollow_symlinksis set), and anything filtered out by ignore / globs / types are skipped.