"""File operations a patch step can return. Paths are relative to vllm/."""

from __future__ import annotations

from dataclasses import dataclass


@dataclass
class Copy:
    """Create (or overwrite) ``dst`` with ``text``."""

    dst: str
    text: str


@dataclass
class Edit:
    """Replace ``old`` with ``new`` in ``path``.

    ``old`` must occur exactly ``count`` times, otherwise the whole patch
    aborts before writing anything. This catches vLLM version drift.
    """

    path: str
    old: str
    new: str
    reason: str
    count: int = 1


def edit_text(text: str, old: str, new: str, reason: str, count: int = 1) -> str:
    """Same check as Edit, for transforming vendored source before Copy."""
    n = text.count(old)
    if n != count:
        raise RuntimeError(f"anchor found {n} times (expected {count}): {reason}")
    return text.replace(old, new)
