name: Test & Audit

on:
  push:
    branches: [ "main", "ci-*" ]
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build_rust:
    name: Build Rust
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Rust
      id: setup-rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: 1.93.0
        components: rustfmt, clippy
    - name: Set up cargo cache
      uses: actions/cache@v4
      continue-on-error: false
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ steps.setup-rust.outputs.cachekey }}
    - name: Install cargo audit
      run: |
        if cargo-audit --help >/dev/null; then
          echo "installed"
        else
          cargo install cargo-audit
        fi
    - name: Build
      run: cargo build --workspace --features strict --verbose

  test_rust:
    name: Run Rust tests
    runs-on: ubuntu-latest
    needs: build_rust
    steps:
    - uses: actions/checkout@v4
    - name: Set up Rust
      id: setup-rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: 1.93.0
        components: rustfmt, clippy
    - name: Set up cargo cache
      uses: actions/cache@v4
      continue-on-error: false
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ steps.setup-rust.outputs.cachekey }}
    - name: Run tests
      run: cargo test --verbose --color=always

  audit_rust:
    name: Audit Rust
    runs-on: ubuntu-latest
    needs: build_rust
    steps:
    - uses: actions/checkout@v4
    - name: Set up Rust
      id: setup-rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: 1.93.0
        components: rustfmt, clippy
    - name: Set up cargo cache
      uses: actions/cache@v4
      continue-on-error: false
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-${{ steps.setup-rust.outputs.cachekey }}
    - name: Install cargo audit
      run: |
        if cargo-audit --help >/dev/null; then
          echo "installed"
        else
          cargo install cargo-audit
        fi
    - name: Cargo audit
      run: cargo audit
    - name: Cargo format check
      run: cargo fmt --check
    - name: Cargo clippy
      run: cargo clippy

  markdown_formatting:
    name: Check markdown formatting
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: PurpleBooth/gfmfmt-action@v2.0.14
      name: Check markdown formatting
