name: CI

on:
  pull_request:
    branches: [main]
  merge_group:
  workflow_dispatch:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Format
    runs-on: namespace-profile-linux-medium
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - uses: Swatinem/rust-cache@v2
      - run: cargo fmt --all -- --check

  rust:
    name: Rust (${{ matrix.os }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu
            runner: namespace-profile-linux-medium
          - os: macos
            runner: namespace-profile-mac-medium
          - os: windows
            runner: namespace-profile-windows-medium
    runs-on: ${{ matrix.runner }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - name: Lint
        run: cargo clippy --all-targets --all-features --locked -- -D warnings
      - name: Test
        run: cargo test --all-targets --all-features --locked
      - name: Build all binaries
        run: cargo build --all-targets --all-features --locked

  e2e:
    name: E2E tests
    runs-on: namespace-profile-linux-medium
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: ./test.sh

  package:
    name: Package dry run
    needs: [rust, e2e]
    runs-on: namespace-profile-linux-medium
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo publish --dry-run --locked
