Skip to main content

Crate recast_core

Crate recast_core 

Source
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§

ApplyOutcome
Returned by apply_changes on success: how many files were written and how many matches they covered.
CompiledPattern
A compiled regex paired with its replacement template. Construct with CompiledPattern::compile; use CompiledPattern::is_convergent to check the idempotency invariant before scanning.
FileChange
One file’s worth of pending rewrite work. after is the full post-image used by crate::apply_changes; diff is the already-rendered unified-diff string. The pre-image is dropped after the diff is built — apply_changes reads from after, not from the original on disk, so retaining the pre-image would just double the planner’s peak memory. permissions is captured during the planner’s metadata read so crate::apply_changes doesn’t have to issue a second fs::metadata syscall just to preserve the mode bits.
PatternOptions
Knobs controlling how a pattern string is compiled into a regex.
Plan
Output of plan_rewrite. Pass to crate::apply_changes to commit.
PlanOptions
Knobs controlling a single plan_rewrite invocation.
RecoverySummary
Summary of a recover_sweep call.
RewriteOutcome
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.
ScriptRewriter
Pre-compiled Rhai script used as a per-match replacement callback.
SearchFile
SearchMatch
SearchOptions
SearchPlan
StructuralOutcome
Result of structural_rewrite: the new source text plus the number of disjoint matches that were rewritten.
WalkOptions
Filters applied while walking roots.
WorkspaceLock
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-all to enable every grammar shipped today.
PlanOutcome
Top-level result classification for a Plan.

Functions§

acquire_workspace_lock
Try to take an exclusive non-blocking lock on lock_path. Returns Error::Locked immediately 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-most match-count guard. Returns Error::TooFewMatches / Error::TooManyMatches when the guard is violated; both variants map to the EXIT_GUARD_VIOLATED (2) exit code at the binary boundary.
compile_friendly_query
Compile a friendly pattern (target-language source with $NAME placeholders) 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 read a/src/a.rs instead of a/./src/a.rs. Absolute paths and plain relative paths pass through unchanged.
plan_rewrite
Walk roots, compile pattern, and produce a Plan of every file that would change when replacement is substituted. Honors the match-count guard, the convergence check, and the file/byte limits in opts. No filesystem writes happen here.
plan_rewrite_scripted
Like plan_rewrite but each match drives a Rhai script callback instead of a static template. The pattern’s replacement field is ignored.
plan_search
plan_structural_rewrite
Multi-file structural pipeline. Walks roots, applies structural_rewrite per file, and folds the results into a Plan that callers can pipe into crate::apply_changes. Honors walk_options, max_files, max_bytes, and the at_least / at_most match-count guard from opts. 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 into SearchPlan.
recover_sweep
Walk every regular file under roots and reconcile leftover .recast.bak.* / .recast.tmp.* siblings from a previous interrupted apply.
rewrite_text
Apply pattern to before and return the rewrite outcome. Counts matches and produces the new text in a single pass via regex::replace_all with an expand-driven closure.
rewrite_text_scripted
Apply pattern to before, calling script once 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 into template per 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_source is written in the target language with $NAME placeholders. 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 source and return match locations without rewriting.
unified_diff
Render a unified diff between before and after with three lines of context, using label for the a/+b/ header paths.
walk_paths
Enumerate every regular file under roots (sorted, dedup’d by the ignore crate) honoring opts. Directories, symlinks (unless follow_symlinks is set), and anything filtered out by ignore / globs / types are skipped.

Type Aliases§

Result