name: ci

on:
  pull_request:
  push:
    branches:
    - main
  workflow_call:
    inputs:
      release:
        description: Whether this is running as part of a release workflow.
        required: false
        type: boolean
        default: false

defaults:
  run:
    shell: bash

env:
  CARGO_INCREMENTAL: 0
  RUSTFLAGS: -Dwarnings

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
        - macos-latest
        - ubuntu-latest
        - windows-latest
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Cache
      uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-${{ runner.arch }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    - name: Set up toolchain
      run: rustup install
    - name: Test
      run: cargo test --all-features --workspace ${{ inputs.release && '--release' || '' }}

  analyze:
    runs-on: ubuntu-latest
    if: ${{ !inputs.release || github.event_name != 'workflow_call' }}
    steps:
    - name: Checkout
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
    - name: Cache
      uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    - name: Set up toolchain
      run: rustup install
    - name: Set up Node.js
      uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
      with:
        node-version: '22'
        cache: 'npm'
    - name: Install Node.js dependencies
      run: npm ci
    - name: Check formatting
      run: cargo fmt --all -- --check
    - name: Lint
      run: cargo clippy --all-features --all-targets --no-deps --workspace
    - name: Check spelling
      run: npx cspell lint --config .cspell.json .
    - name: Lint markdown
      run: npx markdownlint-cli2
    - name: Build documentation
      run: cargo doc --all-features --no-deps --workspace
