name: CI

on:
  pull_request:
  push:
    branches:
      - main
      - master
      - develop

env:
  CARGO_TERM_COLOR: always

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  fmt:
    name: fmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Check formatting
        run: cargo fmt -- --check
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2

  clippy:
    name: clippy
    runs-on: ubuntu-latest
    permissions:
      contents: read
      checks: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Run clippy action
        uses: clechasseur/rs-clippy-check@v4
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2

  doc:
    name: doc
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust nightly
        uses: dtolnay/rust-toolchain@nightly
      - name: Run cargo doc
        run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: --cfg docsrs

  test:
    runs-on: ubuntu-latest
    name: test
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
      - name: cargo generate-lockfile
        if: hashFiles('Cargo.lock') == ''
        run: cargo generate-lockfile
      - name: cargo test --locked
        run: cargo test --locked --all-features --all-targets
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
