name: Rust

on:
  push:
    branches:
      - "*"
  pull_request:
    branches:
      - "*"
  schedule:
    - cron: 0 5 * * 1,5

env:
  CARGO_TERM_COLOR: always

jobs:
  conventions:
    strategy:
      matrix:
        os: ["ubuntu-latest"]
        toolchain: ["stable", "beta", "nightly"]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2

      - name: Install the Rust toolchain
        uses: actions-rs/toolchain@v1
        id: toolchain
        with:
          toolchain: ${{ matrix.toolchain }}
          components: rustfmt, clippy
          override: true
          default: true

      - name: Perform rustfmt checks.
        run: cargo fmt -- --check

      - name: Attempt to restore the compiled artifacts from cache
        uses: actions/cache@v1
        with:
          path: target
          key: ${{ matrix.os }}-${{ steps.toolchain.outputs.rustc_hash }}-build-${{ hashFiles('Cargo.lock') }}

      - name: Perform clippy checks.
        run: cargo clippy --verbose

  testing:
    name: Testing
    strategy:
      matrix:
        os: ["ubuntu-18.04"]
        toolchain: ["stable", "beta", "nightly"]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2

      - name: Install the Rust toolchain
        uses: actions-rs/toolchain@v1
        id: toolchain
        with:
          toolchain: ${{ matrix.toolchain }}
          override: true
          default: true

      - name: Attempt to restore the compiled artifacts from cache
        uses: actions/cache@v1
        with:
          path: target
          key: ${{ matrix.os }}-${{ steps.toolchain.outputs.rustc_hash }}-build-${{ hashFiles('Cargo.lock') }}

      - name: Build
        id: build
        run: cargo build --all-targets --all-features --verbose

      - name: Perform unit testing and integration testing
        run: cargo test --all-targets --all-features -- --ignored

      - name: Perform documentation tests
        run: cargo test --doc

  code_coverage:
    name: Code Coverage
    runs-on: "ubuntu-latest"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

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

      - name: Run cargo-tarpaulin
        uses: actions-rs/tarpaulin@v0.1
        with:
          args: "--verbose --ignored --exclude-files *test* --exclude-files *py*"

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