name: CI

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master
  schedule:
  - cron:  '0 6 * * SAT'

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: stable
        components: rustfmt
    - uses: actions-rs/cargo@v1
      with:
        command: fmt
        args: --all -- --check

  clippy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: stable
        components: clippy
    - uses: actions-rs/clippy-check@v1
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        args: --all-targets -- -D warnings

  audit:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/audit-check@v1
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

  check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: stable
    - uses: actions-rs/cargo@v1
      with:
        command: check
        args: --all

  doc:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: stable
    - uses: actions-rs/cargo@v1
      with:
        command: doc
        args: --all-features --no-deps

  test:
    needs: [fmt, clippy, audit, check, doc]
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
        rust: [stable, nightly]
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: ${{ matrix.rust }}
    - uses: actions-rs/cargo@v1
      with:
        command: test
    - uses: actions-rs/cargo@v1
      with:
        command: test
        args: --no-default-features

  miri:
    needs: [test]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: nightly
        components: miri, rust-src
    - uses: actions-rs/cargo@v1
      with:
        command: miri
        args: setup
    - uses: actions-rs/cargo@v1
      with:
        command: miri
        args: test --all-features
      env:
        MIRIFLAGS: "-Zmiri-disable-isolation"
        PROPTEST_CASES: 1

  sanitize:
    needs: [test]
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        sanitizer: [address, leak, memory, thread]
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: nightly
        components: rust-src
    - uses: actions-rs/cargo@v1
      with:
        command: test
        args: --tests --lib --all-features --target x86_64-unknown-linux-gnu -Z build-std
      env:
        RUSTFLAGS: "-Z sanitizer=${{ matrix.sanitizer }}"

  coverage:
    needs: [test]
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: actions-rs/toolchain@v1
      with:
        override: true
        profile: minimal
        toolchain: stable
    - uses: actions-rs/tarpaulin@v0.1
      with:
        args: --all-features --avoid-cfg-tarpaulin
    - uses: codecov/codecov-action@v2
      with:
        token: ${{secrets.CODECOV_TOKEN}}
        fail_ci_if_error: true
