name: test

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Check on ${{ matrix.rust }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        rust:
          - stable
          - beta
          - nightly
    steps:
      - uses: actions/checkout@v6

      - name: Install ${{ matrix.rust }}
        run: |
          rustup toolchain install ${{ matrix.rust }} --profile minimal --component rustfmt,clippy
          rustup default ${{ matrix.rust }}

      - name: Run cargo check
        run: cargo check

      - name: Run cargo fmt
        run: cargo fmt --all -- --check

      - name: Run cargo clippy
        run: cargo clippy -- -D warnings -W clippy::nursery

      - name: Run cargo test
        run: cargo test --release

  coverage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        run: rustup toolchain install stable --component llvm-tools-preview

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Generate code coverage
        run: cargo llvm-cov --workspace --lcov --output-path lcov.info

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          files: lcov.info
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
