name: CI

on:
  workflow_dispatch:
  pull_request:
    paths-ignore:
      - '**/*.md'
  push:
    branches:
      - main
    paths-ignore:
      - '**/*.md'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref_name != 'main' }}

jobs:
  test:
    name: Check & Test
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
          - os: ubuntu-latest
          - os: macos-latest
          - os: ubuntu-latest
            env:
              CARGO_BUILD_TARGET: wasm32-wasi
              CARGO_TARGET_WASM32_WASI_RUNNER: /home/runner/.wasmtime/bin/wasmtime --dir=.
    runs-on: ${{ matrix.os }}
    env: ${{ matrix.env || fromJSON('{}') }}
    steps:
      - uses: actions/checkout@v3

      - name: Install Wasm deps
        if: matrix.env.CARGO_BUILD_TARGET == 'wasm32-wasi'
        run: |
          rustup target add wasm32-wasi
          curl -LO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk_20.0_amd64.deb
          sudo dpkg --install wasi-sdk_20.0_amd64.deb
          curl https://wasmtime.dev/install.sh -sSf | bash

      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ci
          save-if: ${{ github.ref_name == 'main' }}

      - run: rustup show

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

      - name: Clippy
        run: cargo hack clippy --feature-powerset -- -D warnings

      - name: Test
        run: cargo hack test --feature-powerset

      - name: Check Documentation
        env:
          RUSTDOCFLAGS: '-D warnings'
        run: cargo hack doc --feature-powerset

  format:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - run: rustup show

      - run: cargo fmt --all -- --check

  lint:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ci
          save-if: false

      - run: rustup show

      - run: cargo clippy -- -D warnings
