name: publish-validation

on:
  workflow_dispatch:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  dry-run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      - name: cargo publish (dry-run)
        run: cargo publish --dry-run

      - name: crates.io status (fynch)
        run: |
          python3 - <<'PY'
          import json
          import urllib.error
          import urllib.request
          
          name = "fynch"
          url = f"https://crates.io/api/v1/crates/{name}"
          try:
              with urllib.request.urlopen(url, timeout=10) as resp:
                  data = json.load(resp)
          except urllib.error.HTTPError as e:
              if e.code == 404:
                  print(f"crates.io: {name} not found (ok for first publish)")
                  raise SystemExit(0)
              print(f"crates.io query failed for {name}: HTTP {e.code}: {e}")
              raise SystemExit(1)
          except Exception as e:
              print(f"crates.io query failed for {name}: {e}")
              raise SystemExit(1)
          
          crate = data.get("crate", {})
          print(f"name={crate.get('name')}")
          print(f"max_version={crate.get('max_version')}")
          print(f"downloads={crate.get('downloads')}")
          PY
