name: CI

on:
  push:
    branches:
      - "main"
    paths:
      - ".github/workflows/ci.yaml"
      - "Cargo.toml"
      - "Cargo.lock"
      - "src/**"
      - "tests/**"
  pull_request: {}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - uses: Swatinem/rust-cache@v2

      - name: Lint (rustfmt)
        run: cargo fmt --all --check

      - name: Lint (clippy)
        run: cargo clippy --all-targets --all-features -- -D warnings

  msrv:
    name: MSRV (1.85)
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@1.85

      - uses: Swatinem/rust-cache@v2

      - run: cargo build --all-features --locked

  build:
    name: Build and test
    strategy:
      fail-fast: false
      matrix:
        rust-version:
          - stable
          - beta
        runner:
          - "ubuntu-24.04"
          - "ubuntu-24.04-arm"
    runs-on: ${{ matrix.runner }}

    steps:
      - uses: actions/checkout@v6

      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ matrix.rust-version }}

      - uses: Swatinem/rust-cache@v2

      - uses: taiki-e/install-action@nextest

      - name: Run all tests
        run: cargo nextest run --all-features

      - name: Run doctests
        run: cargo test --doc --all-features

  dependabot:
    needs: [lint, msrv, build]
    runs-on: ubuntu-24.04
    permissions:
      pull-requests: write
      contents: write
    if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request' }}
    steps:
      - id: metadata
        uses: dependabot/fetch-metadata@v2
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      - run: |
          gh pr review --approve "$PR_URL"
          gh pr merge --merge --auto "$PR_URL"
        env:
          PR_URL: ${{ github.event.pull_request.html_url }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
