name: CI

on:
  push:
    branches: [ "main" ]
    paths-ignore:
      - '**.md'
      - 'LICENSE'
  pull_request:
    branches: [ "main" ]
    paths-ignore:
      - '**.md'
      - 'LICENSE'

env:
  CARGO_TERM_COLOR: always

jobs:
  checks:
    name: Check Formatting & Lints
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

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

      - name: Cache Cargo
        uses: Swatinem/rust-cache@v2
                
      - name: Install just
        uses: taiki-e/install-action@just

      - name: Check formatting
        run: cargo fmt --all -- --check

      - name: Run clippy linter
        run: just lint

  test:
    name: Run Tests
    runs-on: ubuntu-latest
    needs: checks
    steps:
      - name: Clean up runner image
        run: |
          df -h
          rm -rf /opt/hostedtoolcache
          sudo rm -rf /usr/local/lib/android
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"
          df -h

      - name: Checkout code
        uses: actions/checkout@v5

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt

      - name: Install protoc
        uses: arduino/setup-protoc@v2
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
        
      - name: Cache Cargo
        uses: Swatinem/rust-cache@v2

      - name: Install just
        uses: taiki-e/install-action@just

      - name: Run offline tests
        run: just test-offline