load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")

package(default_visibility = ["//visibility:public"])

# Hermetic rustfmt step for the sandboxed Rust SDK drift check.
#
# The generator (//.oagen-workspace/retab-gen:generated_rust_sdk_raw) emits an
# UNFORMATTED Rust tree (src/** + Cargo.toml + tests + manifest). rustfmt is not
# in the oagen (Node) generator closure, so this step formats the raw tree with
# the lockfile-pinned, vendored rustfmt binary via //tools/bazel/rustfmt_sdk_tree
# -- mirroring how the Node SDK reformats with its own vendored prettier, Go with
# gofmt, and Python with ruff. rustfmt is NOT self-contained (it dynamically
# loads librustc_driver from the rustc component's lib/), so the pinned rustc
# runtime libs (:rustc_lib) travel with it as run_binary srcs + trailing args;
# the format script points DYLD_LIBRARY_PATH / LD_LIBRARY_PATH at their dir.
#
# The output is scoped to src/** + manifest (Node parity): the raw generator also
# emits Cargo.toml (hand-bumped release version), LICENSE-MIT, README.md and a
# tests/ dir mixing generated + hand-written tests, none of which are part of the
# generated-source contract.
run_binary(
    name = "generated_rust_sdk_formatted",
    srcs = [
        "//.oagen-workspace/retab-gen:generated_rust_sdk_raw",
        "//tools/bazel/rustfmt_sdk_tree:rustfmt",
        "//tools/bazel/rustfmt_sdk_tree:rustc_lib",
    ],
    args = [
        "$(execpath //tools/bazel/rustfmt_sdk_tree:rustfmt)",
        "$(execpath //.oagen-workspace/retab-gen:generated_rust_sdk_raw)",
        "$@",
        "$(execpaths //tools/bazel/rustfmt_sdk_tree:rustc_lib)",
    ],
    out_dirs = ["generated-rust-formatted"],
    tool = "//tools/bazel/rustfmt_sdk_tree:format_rust_sdk_tree",
)

# Stage the committed Rust SDK generated paths into a single TreeArtifact so the
# drift check can diff it against the generated tree entirely inside the sandbox,
# without reading the live working tree. The generated set is exactly the .rs
# files under src/ plus the manifest -- the hand-bumped Cargo.toml/Cargo.lock,
# LICENSE-MIT, README.md and the hand-written integration tests under tests/ are
# NOT generated and are excluded.
copy_to_directory(
    name = "committed_rust_sdk_tree",
    srcs = glob(["src/**/*.rs"]) + [
        ".oagen-manifest.json",
    ],
    out = "committed-rust-sdk-tree",
)
