name: CI

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

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo fmt -- --check
      - run: cargo clippy --all-targets -- -D warnings
      - run: cargo test

  version-sync:
    name: Cargo.toml / flake.nix version match
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Versions must match
        run: |
          CARGO=$(grep -m1 '^version' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
          FLAKE=$(grep -m1 'version = "' flake.nix | sed 's/.*version = "\(.*\)";/\1/')
          echo "Cargo.toml=$CARGO  flake.nix=$FLAKE"
          if [ "$CARGO" != "$FLAKE" ]; then
            echo "::error::Cargo.toml ($CARGO) and flake.nix ($FLAKE) versions differ — keep them in sync."
            exit 1
          fi
