name: Rust

on: [push, pull_request] # note: pre-release job still only on 'refs/heads/main'

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-Dwarnings"
  # Until all marketplace actions ship Node 24 runtimes (see GitHub deprecation of Node 20).
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: default
            features: ""
          - name: no-unsafe
            features: "--features no-unsafe"
    name: Build and Test (${{ matrix.name }})
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v5
    - name: Build (${{ matrix.name }})
      run: cargo build ${{ matrix.features }} --verbose
    - name: Run tests (${{ matrix.name }})
      run: cargo test ${{ matrix.features }} --verbose

  clippy:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: default
            features: ""
          - name: no-unsafe
            features: "--features no-unsafe"
    name: Clippy/Lint check (${{ matrix.name }})
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v5
    - name: run clippy (${{ matrix.name }})
      run: cargo clippy ${{ matrix.features }}

  zip-rules:
    name: zip up the rules directory
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v5
    - name: Remove empty Rules directories
      run:  find Rules -type d -empty -delete
    - name: Remove test directory
      run:  rm -r Rules/Languages/zz
    - name: create rules.zip
      uses: thedoctor0/zip-release@0.7.5
      with:
        type: 'zip'
        filename: 'Rules.zip'
        directory: '.'
        path: 'Rules'
    - name: Upload Rules.zip
      uses: actions/upload-artifact@v4
      with:
        name: 'Rules.zip'
        path: 'Rules.zip'
        compression-level: 0
        retention-days: 1
  
  pre-release:
    name: Pre Release
    continue-on-error: false
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' # otherwise, job fails on pull requests, as GITHUB_TOKEN is not available
    needs: [zip-rules]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
    # download the previous build artifacts and put them in their proper places
    - name: download Rules.zip
      uses: actions/download-artifact@v5
      with:
        name: Rules.zip
    # put the files into the release
    - name: Automatic release
      uses: marvinpinto/action-automatic-releases@latest
      with:
        repo_token: ${{ secrets.GITHUB_TOKEN }}
        automatic_release_tag: latest
        prerelease: true
        title: Development Build
        files: |
          Rules.zip
