name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  validity:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Check source is valid
        run: cargo check --workspace

  formating:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Check formatting with rustfmt
        run: cargo fmt --all --check

  tests:
    needs: validity
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Run all tests
        run: cargo test --workspace --verbose --all-features

  fuzzing:
    needs: validity
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install latest nightly and set it as default
        run: |
          rustup install nightly
          rustup default nightly
      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz
      - name: Run fuzzing
        run: |
          cargo fuzz run -s none module_roundtrip_naive -- -timeout=10 -max_total_time=120 -use_value_profile=1 || true
          cargo fuzz run -s none module_roundtrip_structured -- -timeout=10 -max_total_time=120 -use_value_profile=1 || true
        working-directory: parser/fuzz

  clippy:
    needs: validity
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Lint code with clippy
        run: cargo clippy

  publish-ability:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Check that it will publish to crates
        run: |
          cargo metadata --offline --format-version 1 --no-deps | jq -r ".workspace_members[]" | while read -r _n _v pathInfo ; do
            cd ${pathInfo:13:-1}
            cargo publish --no-verify --dry-run
          done
        shell: bash
