name: Rust

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v3

      - name: Install Protoc
        uses: arduino/setup-protoc@v2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          rustflags: ""

      - name: Build
        run: cargo xtask build

  test:
    name: Test

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v3

      - name: Install Protoc
        uses: arduino/setup-protoc@v2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          rustflags: ""

      - name: Test
        run: cargo xtask test

  test-no-web:
    name: Test (No Web)
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Install Protoc
        uses: arduino/setup-protoc@v2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          rustflags: ""

      - name: Test without web support
        run: cargo xtask test --no-web

  format:
    name: Check Formatting
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          rustflags: ""

      - name: Check Format
        run: cargo xtask format --check
