name: CI

on:
  push:
    branches: [master]
  pull_request:
  schedule:
    - cron: "0 0 * * 1"

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Format, clippy and tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - name: Check formatting
        run: cargo fmt --all -- --check
      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings
      - name: Download iris dataset
        run: curl -sSfO "https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"
      - name: Run tests
        run: cargo test --all-features -- --include-ignored
      - name: Smoke-test benchmarks
        run: cargo bench --bench affinities -- --test

  wasm:
    name: Wasm targets
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown,wasm32-wasip1
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - name: Check wasm32-unknown-unknown with default features
        run: cargo check --target wasm32-unknown-unknown
      - name: Check wasm32-unknown-unknown with the wasm_js feature
        run: cargo check --target wasm32-unknown-unknown --features wasm_js
      - name: Check wasm32-wasip1
        run: cargo check --target wasm32-wasip1
      - name: Clippy on wasm32-unknown-unknown
        run: cargo clippy --target wasm32-unknown-unknown --features wasm_js -- -D warnings

  audit:
    name: Security audit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-audit
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-audit
      - name: Audit dependencies
        run: cargo audit --deny warnings

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-tarpaulin
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-tarpaulin
      - name: Download iris dataset
        run: curl -sSfO "https://gist.githubusercontent.com/curran/a08a1080b88344b0c8a7/raw/0e7a9b0a5d22642a06d3d5b9bcbad9890c8ee534/iris.csv"
      - name: Generate coverage report
        run: cargo tarpaulin --out xml --all-features --ignored --timeout 600
      - name: Upload to codecov
        uses: codecov/codecov-action@v5
        with:
          files: cobertura.xml
