on:
  pull_request: {}
  push:
    branches: main

name: Continuous integration

jobs:
  lint:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        RUST:
          - nightly
    steps:
      - uses: actions/checkout@v2.3.4

      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.RUST }}
          override: true
          components: rustfmt, clippy

      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-${{ matrix.RUST }}-cargo-${{ hashFiles('**/Cargo.toml') }}

      - uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

      - uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --all-targets -- -D warnings

      - uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --all-targets --all-features -- -D warnings

  ci:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        RUST:
          # MSRV
          - VERSION: "1.41.0"
            FLAGS: ""
          - VERSION: stable
            FLAGS: ""
          - VERSION: stable
            FLAGS: "--features const-generics"
          - VERSION: beta
            FLAGS: ""
          - VERSION: beta
            FLAGS: "--features const-generics"
          - VERSION: nightly
            FLAGS: ""
          - VERSION: nightly
            FLAGS: "--features const-generics"

    steps:
      - uses: actions/checkout@v2.3.4

      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.RUST.VERSION }}
          override: true
          components: rustfmt, clippy

      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-${{ matrix.RUST.VERSION }}-cargo-${{ hashFiles('**/Cargo.toml') }}

      - uses: actions-rs/cargo@v1
        with:
          command: test
          args: ${{ matrix.RUST.FLAGS }}

      - uses: actions-rs/cargo@v1
        with:
          command: check
          args: ${{ matrix.RUST.FLAGS }}

  fuzz:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        RUST:
          - nightly

    steps:
      - uses: actions/checkout@v2.3.4

      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ matrix.RUST }}
          override: true

      - uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}

      - uses: actions-rs/install@v0.1
        with:
          crate: cargo-fuzz
          version: latest

      - uses: actions-rs/cargo@v1
        with:
          command: fuzz
          args: run --all-features fuzz_asn1_parse -- -max_total_time=30

      # On masin we fuzz for 10 minutes, but only 30 seconds on PRs.
      - uses: actions-rs/cargo@v1
        with:
          command: fuzz
          args: run --all-features fuzz_asn1_parse -- -max_total_time=600
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
