name: CI

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  rust:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false

      - name: Install Rust
        uses: dtolnay/rust-toolchain@4be7066ada62dd38de10e7b70166bc74ed198c30 # stable
        with:
          components: clippy, rustfmt

      - name: Cache cargo registry/build
        uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2

      - name: Format check
        run: cargo fmt --all -- --check

      - name: Clippy
        run: cargo clippy --all-targets -- -D warnings

      - name: Test
        run: cargo test

  nix:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            system: x86_64-linux
          - os: ubuntu-24.04-arm
            system: aarch64-linux
          - os: macos-latest
            system: aarch64-darwin
          - os: macos-15-intel
            system: x86_64-darwin
    runs-on: ${{ matrix.os }}
    env:
      CACHIX_AUTH_TOKEN: ${{ secrets.CACHIX_AUTH_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false

      - name: Setup Nix
        uses: ./.github/actions/setup-nix

      - name: Flake check
        run: nix flake check --no-build

      - name: Build sofka
        run: nix build .#sofka --print-build-logs

      - name: Smoke-test the built binary
        run: ./result/bin/sofka --version
