name: check
on:
  pull_request:
    branches: [main]
permissions:
  contents: read
  id-token: write
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      rust: ${{ steps.changes.outputs.rust }}
      web: ${{ steps.changes.outputs.web }}
    steps:
      - uses: actions/checkout@v6
      - uses: dorny/paths-filter@v4
        id: changes
        with:
          filters: |
            rust:
              - 'flake.nix'
              - 'flake.lock'
              - '.github/workflows/check.yml'
              - 'wit/**'
              - 'src/**'
              - 'Cargo.{toml,lock}'
              - '.cargo/**'
              - 'rustfmt.toml'
            web:
              - 'flake.nix'
              - 'flake.lock'
              - '.github/workflows/check.yml'
              - 'wit/**'
              - 'web/**'
  fmt:
    runs-on: ubuntu-latest
    needs: changes
    outputs:
      rust: ${{ needs.changes.outputs.rust }}
      web: ${{ needs.changes.outputs.web }}
    steps:
      - uses: actions/checkout@v6
      - uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/flakehub-cache-action@main
      - run: nix flake check
  rust-check:
    runs-on: ubuntu-latest
    needs: changes
    if: ${{ needs.changes.outputs.rust == 'true' }}
    steps:
      - uses: actions/checkout@v6
      - uses: DeterminateSystems/determinate-nix-action@main
      - uses: DeterminateSystems/flakehub-cache-action@main
      - run: nix build
  web-check:
    runs-on: ubuntu-latest
    needs: changes
    if: ${{ needs.changes.outputs.web == 'true' }}
    defaults:
      run:
        working-directory: web
    steps:
      - uses: actions/checkout@v6
      - uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/flakehub-cache-action@main
      - uses: pnpm/action-setup@v6
        with:
          version: latest
      - uses: actions/setup-node@v6
        with:
          node-version: "*"
          cache: pnpm
          cache-dependency-path: web/pnpm-lock.yaml
      - run: pnpm ci
      - run: pnpm build
      - run: pnpm check
      - run: pnpm lint
