name: CI

on:
  push:
    branches: [v1]
  pull_request:
  workflow_dispatch:
  merge_group:
    types: [checks_requested]

jobs:
  ci:
    name: Build/Test
    strategy:
      matrix:
        toolchain: ["stable", "beta", "nightly", "1.36.0"]
        os: [ubuntu-latest]
        include:
          - toolchain: stable
            fuzz: 0
          - toolchain: beta
            fuzz: 0
          - os: windows-latest
            toolchain: nightly

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

      - name: Install packages for fuzzing
        if: runner.os == 'Linux' && matrix.fuzz == 1
        run: sudo apt-get update -y && sudo apt-get install -y binutils-dev libunwind8-dev libcurl4-openssl-dev libelf-dev libdw-dev cmake gcc libiberty-dev

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.toolchain }}

      - name: Cargo build
        run: cargo build --verbose

      - name: Cargo test
        if: matrix.toolchain != '1.36.0'
        run: cargo test --verbose

      - name: Cargo test w/ serde
        if: matrix.toolchain != '1.36.0'
        run: cargo test --verbose --features serde

      - name: Cargo test w/ malloc_size_of
        if: matrix.toolchain != '1.36.0'
        run: cargo test --verbose --features malloc_size_of

      - name: Cargo check w/o default features
        if: matrix.toolchain == 'nightly'
        run: cargo check --verbose --no-default-features

      - name: Cargo test w/ union
        if: matrix.toolchain == 'beta'
        run: cargo test --verbose --features union

      - name: Cargo test w/ debugger_visualizer
        if: matrix.toolchain == 'nightly'
        run: cargo test --test debugger_visualizer --verbose --features debugger_visualizer -- --test-threads=1

      - name: Cargo test w/ debugger_visualizer and union
        if: matrix.toolchain == 'nightly'
        run: cargo test --test debugger_visualizer --verbose --features 'debugger_visualizer,union' -- --test-threads=1

      - name: Cargo test all features
        if: matrix.toolchain == 'nightly'
        run: cargo test --verbose --all-features

      - name: Cargo bench
        if: matrix.toolchain == 'nightly'
        run: cargo bench --verbose bench

      - name: miri
        if: matrix.toolchain == 'nightly' && matrix.os == 'ubuntu-latest'
        run: bash ./scripts/run_miri.sh

      - name: fuzz
        if: matrix.fuzz == 1
        working-directory: fuzz
        run: ./travis-fuzz.sh

  no-std:
    name: no_std
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          target: thumbv7m-none-eabi
      - name: Cargo build
        run: cargo build --verbose

  build_result:
    name: Result
    runs-on: ubuntu-latest
    needs:
      - "ci"
      - "no-std"

    steps:
      - name: Mark the job as successful
        run: exit 0
        if: success()
      - name: Mark the job as unsuccessful
        run: exit 1
        if: "!success()"
