name: Rust

env:
  CARGO_TERM_COLOR: always

permissions:
  contents: read

on:
  push:
    branches:
      - master
      - dev
    paths:
      - ".github/workflows/*.yml"
      - "Cargo.toml"
      - "src/**.rs"
      - "tests/**.rs"
  pull_request:
    branches:
      - master
      - dev
    paths:
      - ".github/workflows/*.yml"
      - "Cargo.toml"
      - "src/**.rs"
      - "tests/**.rs"

jobs:
  lint:
    runs-on: ubuntu-latest
    name: Lint
    permissions:
      checks: write
      contents: write
      pull-requests: write
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          default: true
          profile: minimal
          components: rustfmt, clippy
      - name: Run cargo fmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --check --verbose
      - name: Run cargo clippy
        uses: actions-rs/clippy-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          args: --all-features -- --deny clippy::cargo

  deny:
    runs-on: ubuntu-latest
    name: Deny
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          default: true
          profile: minimal
      - name: Install cargo deny
        uses: actions-rs/install@v0.1
        with:
          crate: cargo-deny
      - name: Run cargo deny
        uses: actions-rs/cargo@v1
        with:
          command: deny
          args: --all-features check

  security-audit:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          default: true
          profile: minimal
      - name: Run audit check
        uses: actions-rs/audit-check@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  build-and-test-linux:
    needs:
      - deny
      - lint
      - security-audit
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        toolchain: [1.66.0, stable, nightly]
    name: "Build and test (OS: Linux, Toolchain: ${{ matrix.toolchain }})"
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.toolchain }}
          default: true
          profile: minimal
      - name: Run cargo build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --all-features --verbose
      - name: Run cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --all-features --verbose

  build-and-test-macos:
    needs:
      - deny
      - lint
      - security-audit
    runs-on: macos-latest
    strategy:
      fail-fast: false
      matrix:
        toolchain: [1.66.0, stable, nightly]
    name: "Build and test (OS: MacOS, Toolchain: ${{ matrix.toolchain }})"
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.toolchain }}
          default: true
          profile: minimal
      - name: Run cargo build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --all-features --verbose
      - name: Run cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --all-features --verbose

  build-and-test-windows:
    needs:
      - deny
      - lint
      - security-audit
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        toolchain: [1.66.0, stable, nightly]
    name: "Build and test (OS: Windows, Toolchain: ${{ matrix.toolchain }})"
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: ${{ matrix.toolchain }}
          default: true
          profile: minimal
      - name: Run cargo build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --all-features --verbose
      - name: Run cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --all-features --verbose

  coverage:
    needs:
      - deny
      - lint
      - security-audit
    runs-on: ubuntu-latest
    name: Coverage
    steps:
      - name: Repository checkout
        uses: actions/checkout@v3
      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: nightly
          default: true
          profile: minimal
      - name: Install cargo tarpaulin
        uses: actions-rs/install@v0.1
        with:
          crate: cargo-tarpaulin
      - name: Run cargo tarpaulin
        run: cargo tarpaulin --all-features --engine llvm --fail-under 50 --follow-exec --ignore-tests --out xml --timeout 120
      - name: Upload to codecov.io
        if: ${{ always() }}
        uses: codecov/codecov-action@v3
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true
