name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: ["**"]

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v1

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Run cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        build: [beta, stable, windows, macos]
        include:
          - build: macos
            os: macos-latest
            rust: stable
          - build: windows
            os: windows-latest
            rust: stable
          - build: beta
            os: ubuntu-latest
            rust: beta
          - build: stable
            os: ubuntu-latest
            rust: stable
    steps:
      - uses: actions/checkout@v1

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          override: true

      - name: Run tests
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --no-fail-fast

  test_all_feature_combinations:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - uses: actions-rs/install@v0.1
        with:
          crate: cargo-all-features
          version: latest
          use-tool-cache: true

      - name: Run tests for all feature combinations
        run: cargo test-all-features --no-fail-fast --all

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

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          override: true

      - name: Install cargo-tarpaulin
        uses: actions-rs/install@v0.1
        with:
          crate: cargo-tarpaulin
          version: latest
          use-tool-cache: true

      - name: Run cargo-tarpaulin
        run: cargo tarpaulin --all-features --timeout 600 --out Xml -- --test-threads 1

      - name: Upload coverage to codecov
        uses: codecov/codecov-action@v1.0.3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

  lints:
    name: Lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v1

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      - name: Run cargo fmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

      - name: Run cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -D warnings

  publish:
    name: Publish (dry-run)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v1

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - name: Make sure we can publish the crate
        uses: actions-rs/cargo@v1
        with:
          command: publish
          args: --dry-run

  minimum_rust_version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: 1.40.0
          override: true

      - name: check if README matches MSRV defined here
        run: grep '1.40.0' README.md

      - name: Run tests
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --all-features --no-fail-fast

  wasm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          target: wasm32-unknown-unknown

      - name: Build on wasm
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --target wasm32-unknown-unknown --no-default-features --features gz
