Documentation
name: build

on: [push, pull_request]

jobs:
  build:
    name: Build
    strategy:
      fail-fast: false
      matrix:
        platform: [ubuntu-latest, macos-latest, windows-latest]
        toolchain: [stable]
    runs-on: ${{ matrix.platform }}

    steps:
      - name: Checkout Sources
        uses: actions/checkout@v3

      - name: Cache Dependencies & Build Outputs
        uses: actions/cache@v3
        with:
          path: ~/.cargo
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

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

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

      - name: Code Lint
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --all-targets --all-features -- -D warnings

      - name: Test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --all-features

  build-no-std:
    name: Build no_std
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Sources
        uses: actions/checkout@v3

      - name: Install Rust Toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          target: thumbv6m-none-eabi

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --no-default-features --target thumbv6m-none-eabi

  build-no-std-serde:
    name: Build no_std, but with `serde-codec` feature enabled
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Sources
        uses: actions/checkout@v3

      - name: Install Rust Toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable

      - name: Build
        uses: actions-rs/cargo@v1
        with:
          command: build
          # `thumbv6m-none-eabi` can't be used as Serde doesn't compile there.
          args: --no-default-features --features serde-codec

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Sources
        uses: actions/checkout@v3

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

      - name: Generate Code Coverage
        uses: actions-rs/tarpaulin@v0.1
        with:
          args: --all-features

      - name: Upload Code Coverage
        uses: codecov/codecov-action@v3