name: CI

on:
  pull_request:
    paths:
      - "**"
      - "!/*.md"
      - "!/docs/**"
      - "!/LICENSE"
      - "!/README*"
  push:
    branches:
      - main
      - dev
    paths:
      - "**"
      - "!/*.md"
      - "!/docs/**"
      - "!/LICENSE"
      - "!/README*"
  schedule:
    - cron: "3 3 3 * *"

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  ci:
    name: CI
    needs: [test, check, rustfmt, clippy, docs, coverage]
    if: always()
    runs-on: ubuntu-latest
    steps:
      - name: Check all jobs passed
        run: |
          if [[ "${{ needs.test.result }}" != "success" ]] || \
             [[ "${{ needs.check.result }}" != "success" ]] || \
             [[ "${{ needs.rustfmt.result }}" != "success" ]] || \
             [[ "${{ needs.docs.result }}" != "success" ]]; then
            echo "One or more jobs failed"
            exit 1
          fi
          echo "All jobs passed"

  test:
    name: Test (${{ matrix.name }})
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: linux
            os: ubuntu-latest
            rust: stable
          - name: windows
            os: windows-latest
            rust: stable
          - name: mac
            os: macos-latest
            rust: stable
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: ${{ matrix.name }}

      - name: Strip path dependencies
        uses: ./.github/actions/strip-path-deps

      - name: Build
        run: cargo build --workspace --all-targets

      - name: Test
        run: cargo test --workspace

  check:
    name: Check (${{ matrix.name }})
    runs-on: ubuntu-latest
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: debug
            rust: stable
            flags: ""
          - name: release
            rust: stable
            flags: "--release"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: check-${{ matrix.name }}

      - name: Strip path dependencies
        uses: ./.github/actions/strip-path-deps

      - name: Check
        run: cargo check --workspace --all-targets ${{ matrix.flags }}

  docs:
    name: Documentation
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2

      - name: Strip path dependencies
        uses: ./.github/actions/strip-path-deps

      - name: Check documentation
        env:
          RUSTDOCFLAGS: -D warnings
        run: cargo doc --workspace --no-deps --document-private-items

  rustfmt:
    name: Format
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - name: Check formatting
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2

      - name: Strip path dependencies
        uses: ./.github/actions/strip-path-deps

      - name: Lint
        run: cargo clippy --workspace --all-targets -- -D warnings -A deprecated

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

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

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2

      - name: Strip path dependencies
        uses: ./.github/actions/strip-path-deps

      - name: Collect coverage data
        run: cargo llvm-cov --workspace --lcov --no-cfg-coverage --output-path lcov.info

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