name: Continuous integration

on:
  pull_request:
  push:
    branches:
      - master

jobs:
  check-style:
    name: Check style
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Install Rust stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: clippy, rustfmt

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

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

      - name: Cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -A clippy::mutable_key_type

  check-code:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

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

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

      - name: Cargo check no-default-features
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --benches --tests

      - name: Cargo check HTTP client with hyper
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --manifest-path http-client/Cargo.toml --no-default-features --features hyper-client

      - name: Cargo check HTTP client for WASM.
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --manifest-path http-client/Cargo.toml --no-default-features --features wasm-client --target wasm32-unknown-unknown

  tests:
    name: Run tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v2

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

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

      - name: Cargo build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --workspace

      - name: Cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
