name: Rust

on:
  pull_request:

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macOS-latest]
        toolchain: [stable]
        include:
          - os: ubuntu-latest
            toolchain: "1.74"
    defaults:
      run:
        working-directory: ./safetensors

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2

      - name: Install Rust Stable
        uses: dtolnay/rust-toolchain@29eef336d9b2848a0b548edc03f92a220660cdb8  # stable
        with:
          components: rustfmt, clippy, llvm-tools-preview
          override: true

      - uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32  # v2

      - name: Install cargo-audit
        run: cargo install cargo-audit

      - name: Build
        run: cargo build --all-targets --verbose

      - name: Lint with Clippy
        run: cargo clippy --all-targets -- -D warnings

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

      - name: Run No-STD Tests
        run: cargo test --no-default-features --verbose

      - name: Run Audit
        # RUSTSEC-2021-0145 is criterion so only within benchmarks
        run: cargo audit -D warnings --ignore RUSTSEC-2021-0145
