name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]

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

    steps:
    - uses: actions/checkout@v5
      with:
        submodules: recursive

    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        components: rustfmt, clippy

    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-

    - name: Check formatting
      run: cargo fmt --check

    - name: Run clippy
      run: cargo clippy --locked -- -D warnings

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

    - name: Check publishable
      if: runner.os == 'Linux'
      run: cargo publish --locked --dry-run

  docker-e2e:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v5
      with:
        submodules: recursive

    - name: Build and run Docker e2e test
      run: |
        docker build -t pxh-e2e -f tests/docker/Dockerfile .
        docker run --rm pxh-e2e

  msrv:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v5
      with:
        submodules: recursive

    - name: Install MSRV Rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: "1.95"

    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-msrv-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-msrv-

    - name: Build with MSRV
      run: cargo build --locked
