name: Rust

env:
  MSRV: "1.70"

on: [push, pull_request]

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@beta
        with:
          components: clippy, rustfmt
      - uses: Swatinem/rust-cache@v2
      - name: cargo fmt
        run: cargo fmt --all --check
      - name: clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  check-docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: cargo doc
        env:
          RUSTDOCFLAGS: "-D rustdoc::all -A rustdoc::private-doc-tests"
        run: cargo doc --all-features --no-deps

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Check rustc and cargo version
        run: rustc -V && cargo -V
      - name: Build
        run: cargo build --all-features --all-targets --verbose
      - name: Run tests
        run: cargo test --all-features --all-targets --verbose
      - name: Build (no default features)
        run: cargo build --no-default-features --all-targets --verbose
      - name: Run tests (no default features)
        run: cargo test --no-default-features --all-targets --verbose

  test-freebsd:
    name: Test (FreeBSD)
    runs-on: ubuntu-latest
    defaults:
      run:
          shell: cpa.sh {0}
    steps:
      - uses: actions/checkout@v4
      - name: Test on FreeBSD
        uses: cross-platform-actions/action@v1.3.0
        with:
          operating_system: freebsd
          version: "15.1"
      - run: |
          sudo pkg install -y rust
          cargo test

  miri:
    name: "Miri"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Miri
        run: |
          rustup toolchain install nightly --component miri
          rustup override set nightly
          cargo miri setup
      - name: Test with Miri tests with "from_buffer" in name
        run: cargo miri test from_buffer
