name: CI
on:
  pull_request:
  push:

permissions:
  contents: read

env:
  RUST_BACKTRACE: full
  RUSTDOCFLAGS: -Dwarnings
  RUSTFLAGS: -Dwarnings

jobs:
  style:
    name: Code style
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust beta
        run: |
          rustup update --no-self-update beta
          rustup default beta

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Rustfmt
        run: cargo fmt -- --check

      - name: Clippy
        run: cargo clippy --workspace --all-targets -- -D clippy::dbg_macro -D clippy::todo

      - name: Rustdoc
        run: cargo doc --workspace --no-deps

  test:
    strategy:
      fail-fast: false
      matrix:
        rust: [stable, beta]
    name: Test ${{ matrix.rust }}
    # Need libsqlite3-dev >= 3.38.0 (2022-02-22)
    runs-on: ubuntu-24.04
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install sqlite
        run: sudo apt-get install --no-install-recommends -y libsqlite3-dev sqlite3

      - name: Disable high-priority rust-toolchain.toml
        run: rm rust-toolchain.toml

      - name: Install Rust ${{ matrix.rust }}
        run: |
          rustup update --no-self-update ${{ matrix.rust }}
          rustup default ${{ matrix.rust }}

      - name: Disable webapi tests on stable rustc
        if: matrix.rust == 'stable'
        run: rm ./blahd/tests/webapi.rs

      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v2

      - name: Build
        run: cargo build --workspace --all-targets

      - name: Test
        run: cargo test --workspace --all-targets

        # WAIT: Next release of `criterion` for `--include-ignored`.
      - name: Test ignored
        run: cargo test --workspace --all-targets -- --ignored

  nix-flake:
    name: Flake package
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install nix
        uses: cachix/install-nix-action@v26
        with:
          github_access_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Flake check
        run: nix flake check --no-update-lock-file --show-trace

      - name: Flake build
        run: nix build --no-update-lock-file --show-trace --print-build-logs