name: CI

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

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build & Test
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      # - name: Cache cargo registry
      #   uses: actions/cache@v4
      #   with:
      #     path: |
      #       ~/.cargo/registry
      #       ~/.cargo/git
      #       target
      #     key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      #     restore-keys: |
      #       ${{ runner.os }}-cargo-

      - name: Build
        run: cargo build --verbose

      - name: Run tests
        run: cargo test --verbose

  fmt:
    name: Format
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          components: rustfmt

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