stellar-xdr 27.0.0

Stellar XDR types, encoding, and decoding.
Documentation
name: Rust

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref_protected == 'true' && github.sha || github.ref }}-{{ github.event_name }}
  cancel-in-progress: true

env:
  CARGO_HACK_ARGS: --feature-powerset --exclude-features default --group-features base64,serde,serde_json,schemars,arbitrary,hex,rand

jobs:

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

  generate:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        submodules: true
    - run: rustup update
    - run: make clean generate
    - shell: 'script --return --quiet --command "bash {0}"'
      run: git diff --exit-code || (echo "Generated Rust code is out-of-date with the XDR .x files.\nRegenerate and test them by running 'make clean generate build test'." && exit 1)

  fmt:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - run: rustup update
    - run: cargo fmt --all --check

  readme:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: stellar/actions/rust-cache@main
    - run: rustup install nightly
    - run: make readme
    - run: git add -N . && git diff HEAD --exit-code

  # Expand the cargo-hack feature powerset into a list of individual cargo
  # invocations, so the build and test jobs below can run each feature
  # combination as its own parallel job. Running them separately keeps each
  # runner from accumulating the build artifacts of every combination, which
  # was exhausting disk space when the whole powerset ran on one runner.
  prepare:
    runs-on: ubuntu-latest
    outputs:
      feature-args: ${{ steps.gen.outputs.feature-args }}
      clippy-matrix: ${{ steps.gen.outputs.clippy-matrix }}
    steps:
    - uses: actions/checkout@v3
    - run: rustup update
    - uses: stellar/binaries@v55
      with:
        name: cargo-hack
        version: 0.6.35
    - id: gen
      # Strip the leading `cargo test ` from each printed command, leaving the
      # per-combination feature args reused by the test job, and emit them as a
      # JSON array. From those, derive the clippy build matrix: the wasm target
      # runs minimal clippy once, and the x86 target runs a job per feature
      # combination (with --all-targets).
      run: |
        feature_args="$(cargo hack test $CARGO_HACK_ARGS --print-command-list | sed 's/^cargo test //' | jq -R . | jq -sc .)"
        echo "feature-args=$feature_args" >> "$GITHUB_OUTPUT"
        echo "clippy-matrix=$(printf '%s' "$feature_args" | jq -c '
          [{target:"wasm32v1-none", flags:"--target wasm32v1-none --no-default-features"}]
          + map({target:"x86_64-unknown-linux-gnu", flags:(. + " --target x86_64-unknown-linux-gnu --all-targets")})
        ')" >> "$GITHUB_OUTPUT"

  build:
    needs: prepare
    strategy:
      fail-fast: false
      matrix:
        rust: [msrv, latest]
        sys: ${{ fromJSON(needs.prepare.outputs.clippy-matrix) }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: stellar/actions/rust-cache@main
    - name: Use the minimum supported Rust version
      if: matrix.rust == 'msrv'
      run: |
        msrv="$(cargo metadata --format-version 1 --no-deps | jq -r '.packages | map(.rust_version) | map(values) | min')"
        rustup override set $msrv
        rustup component add clippy --toolchain $msrv
    - name: Error on warnings and clippy checks
      # Only error on warnings and checks for the msrv, because new versions of
      # Rust will frequently add new warnings and checks.
      if: matrix.rust == 'msrv'
      run: echo RUSTFLAGS='-Dwarnings -Dclippy::all -Dclippy::pedantic' >> $GITHUB_ENV
    - run: rustup update
    - run: rustup target add ${{ matrix.sys.target }}
    - run: cargo version
    - run: cargo clippy ${{ matrix.sys.flags }}

  test:
    needs: prepare
    strategy:
      fail-fast: false
      matrix:
        rust: [msrv, latest]
        feature-args: ${{ fromJSON(needs.prepare.outputs.feature-args) }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - uses: stellar/actions/rust-cache@main
    - name: Use the minimum supported Rust version
      if: matrix.rust == 'msrv'
      run: |
        msrv="$(cargo metadata --format-version 1 --no-deps | jq -r '.packages | map(.rust_version) | map(values) | min')"
        rustup override set $msrv
    - name: Error on warnings and clippy checks
      # Only error on warnings and checks for the msrv, because new versions of
      # Rust will frequently add new warnings and checks.
      if: matrix.rust == 'msrv'
      run: echo RUSTFLAGS='-Dwarnings -Dclippy::all -Dclippy::pedantic' >> $GITHUB_ENV
    - run: rustup update
    - run: cargo version
    - run: cargo test ${{ matrix.feature-args }}

  publish-dry-run:
    if: github.event_name == 'push' || startsWith(github.head_ref, 'release/')
    strategy:
      matrix:
        sys:
        - os: ubuntu-latest
          target: wasm32-unknown-unknown
          cargo-hack-feature-options: --manifest-path Cargo.toml --each-feature --exclude-features cli,rand
        - os: ubuntu-latest
          target: wasm32v1-none
          cargo-hack-feature-options: --manifest-path Cargo.toml --exclude-features default,cli,rand,schemars,serde,alloc,arbitrary,base64,hex,serde_json,std --each-feature
        - os: ubuntu-latest
          target: x86_64-unknown-linux-gnu
          cargo-hack-feature-options: --feature-powerset --exclude-features default --group-features base64,serde,serde_json,schemars,arbitrary,hex,rand
    uses: stellar/actions/.github/workflows/rust-publish-dry-run.yml@main
    with:
      runs-on: ${{ matrix.sys.os }}
      target: ${{ matrix.sys.target }}
      cargo-hack-feature-options: ${{ matrix.sys.cargo-hack-feature-options }}