Expand description
Surgical source rewriting: replace exactly one syn::Item’s byte span
with a re-printed, mutated version of itself, leaving the rest of the
file byte-identical.
taint-generate and taint-refactor both need to add an attribute (or
insert a call) to one specific fn/mod inside a real source file
without disturbing anything else in it. A full syn::parse_file +
prettyplease::unparse round-trip of the whole file would work, but
it silently reformats every other item and strips blank-line structure
wherever syn doesn’t preserve it — an unacceptable side effect for a
tool whose entire job is “add one attribute, touch nothing else”. This
crate instead: (1) locates the exact byte range of the one item being
changed via span_byte_range (using proc_macro2’s span-locations
line/column data, since this always runs outside a real proc-macro), (2)
re-prints just that mutated item alone via prettyplease
(print_item), and (3) splices it back into the original text
(apply_edits). Everything outside a changed item’s own span is
untouched; the changed item itself may come out less indented than its
surrounding context if it was nested (prettyplease always prints from
column zero) — recommend running cargo fmt after applying edits.
Structs§
- Source
Edit - One pending replacement: swap the original source’s
[start, end)byte range forreplacement.
Functions§
- apply_
edits - Apply every edit to
source, returning the rewritten text. - byte_
offset - Convert a 1-indexed line number and 0-indexed, char-counted column
(
proc_macro2::LineColumn’s own convention) into a byte offset. - parse_
attribute - Parse a single standalone
#[...]outer attribute fromtext. - print_
item - Pretty-print
itemalone (wrapped in a bare, attribute-lesssyn::File) viaprettyplease, trimmed of its trailing newline so callers can splice it directly into aSourceEdit::replacement. - span_
byte_ range - The
[start, end)byte rangespancovers insource.