name: CI

on:
  push:
    branches:
      - main
    tags-ignore:
      - v*
    paths-ignore:
      - 'LICENSE'
      - '**.md'
  pull_request:
    paths-ignore:
      - 'LICENSE'
      - '**.md'

env:
  CARGO_INCREMENTAL: 0

jobs:
  format:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Cargo fmt
        run: cargo fmt --all -- --check

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Cache cargo registry
        uses: actions/cache@v1
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo index
        uses: actions/cache@v1
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo build
        uses: actions/cache@v1
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
      - name: Add clippy
        run: rustup component add clippy
      - name: Run lint
        run: cargo clippy

  unit_tests:
    name: Unit Tests
    runs-on: ${{ matrix.os }}
    env:
      # For some builds, we use cross to test on 32-bit and big-endian
      # systems.
      CARGO: cargo
      # When CARGO is set to CROSS, this is set to `--target matrix.target`.
      TARGET_FLAGS: ""
      # When CARGO is set to CROSS, TARGET_DIR includes matrix.target.
      TARGET_DIR: ./target
      # Emit backtraces on panics.
      RUST_BACKTRACE: 1
      RUST_MIN_SRV: "1.43.1"
    strategy:
      fail-fast: false
      matrix:
        build: [linux, linux-arm, macos, win-msvc, win32-msvc]
        include:
        - build: linux
          os: ubuntu-18.04
          rust: stable
          target: x86_64-unknown-linux-musl
        - build: linux-arm
          os: ubuntu-18.04
          rust: stable
          target: arm-unknown-linux-gnueabihf
        - build: macos
          os: macos-latest
          rust: stable
          target: x86_64-apple-darwin
        - build: win-msvc
          os: windows-2019
          rust: stable
          target: x86_64-pc-windows-msvc
        - build: win32-msvc
          os: windows-2019
          rust: stable
          target: i686-pc-windows-msvc
    steps:
      - uses: actions/checkout@v2

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.rust }}
          profile: minimal
          override: true
          target: ${{ matrix.target }}

      - name: Use Cross
        shell: bash
        run: |
          cargo install cross
          echo "CARGO=cross" >> $GITHUB_ENV
          echo "TARGET_FLAGS=--target ${{ matrix.target }}" >> $GITHUB_ENV
          echo "TARGET_DIR=./target/${{ matrix.target }}" >> $GITHUB_ENV

      - name: Show command used for Cargo
        run: |
          echo "cargo command is: ${{ env.CARGO }}"
          echo "target flag is: ${{ env.TARGET_FLAGS }}"
          echo "target dir is: ${{ env.TARGET_DIR }}"

      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v1

      - name: Build
        run: ${{ env.CARGO }} build --verbose ${{ env.TARGET_FLAGS }}

      - name: Run tests
        run: ${{ env.CARGO }} test --verbose --workspace ${{ env.TARGET_FLAGS }}
