name: Rust

on:
  push:
    branches: [main]
  pull_request:

env:
  RUSTFLAGS: -D warnings

jobs:

  complete:
    if: always()
    needs: [fmt, build-and-test, publish-dry-run]
    runs-on: ubuntu-latest
    steps:
    - if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
      run: exit 1

  fmt:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - run: rustup update stable && rustup default stable
    - run: find . -type f -name '*.rs' -print0 | xargs -I {} -0 rustfmt --check "{}"

  build-and-test:
    strategy:
      matrix:
        include:
        - os: ubuntu-latest
          target: x86_64-unknown-linux-gnu
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v3
    - uses: stellar/actions/rust-cache@main
    - run: rustup update
    - run: cargo build --target ${{ matrix.target }}
    - run: cargo test --target ${{ matrix.target }}

  publish-dry-run:
    if: startsWith(github.head_ref, 'release/')
    strategy:
      matrix:
        include:
        - os: ubuntu-latest
          target: x86_64-unknown-linux-gnu
    uses: stellar/actions/.github/workflows/rust-publish-dry-run.yml@main
    with:
      runs-on: ${{ matrix.os }}
      target: ${{ matrix.target }}
