name: Rust CI

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

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

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

      - name: Run cargo test
        run: cargo test --verbose

  fuzz:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install nightly toolchain
        uses: dtolnay/rust-toolchain@nightly

      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz

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

      - name: Run fuzzing
        run: |
          for target in $(cargo fuzz list); do
            echo "Fuzzing target: $target"
            cargo +nightly fuzz run $target -- -max_total_time=20 -rss_limit_mb=4096 -max_len=524288 -runs=10000
          done
