#!/usr/bin/env bash
set -euo pipefail

# Generates a lightweight public API report via rustdoc JSON.
# Requires nightly rustdoc.

RUSTDOCFLAGS="-Zunstable-options --output-format json" \
  cargo +nightly doc --no-deps --lib -Z unstable-options

JSON="target/doc/jvmti_bindings.json"
OUT="docs/PUBLIC_API_REPORT.md"

python - <<'PY'
import json
from pathlib import Path

json_path = Path("target/doc/jvmti_bindings.json")
if not json_path.exists():
    raise SystemExit("rustdoc json not found: " + str(json_path))

j = json.loads(json_path.read_text())

# Very lightweight report: list top-level exports by name.
# We keep this simple to avoid a heavy dependency on rustdoc internals.

crate_index = j.get("index", {})

items = []
for k, v in crate_index.items():
    if v.get("crate_id") == 0 and v.get("visibility") == "public":
        name = v.get("name")
        if name:
            items.append(name)

items = sorted(set(items))

out = Path("docs/PUBLIC_API_REPORT.md")
lines = [
    "# Public API Report",
    "",
    "(Generated by scripts/public_api_report.sh)",
    "",
    "Top-level public items:",
]
lines.extend([f"1. {name}" for name in items])

out.write_text("\n".join(lines) + "\n")
print(f"Wrote {out}")
PY
