on:
  push:
    branches:
      - main
    paths:
      - '**/Cargo.toml'
      - '**/Cargo.lock'
      - '**/rust-toolchain.toml'
      - '**.rs'
      - .github/workflows/cargo-fmt.yml
  pull_request:
    paths:
      - '**/Cargo.toml'
      - '**/Cargo.lock'
      - '**/rust-toolchain.toml'
      - '**.rs'
      - .github/workflows/cargo-fmt.yml
permissions:
  packages: read
  contents: read
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true
name: cargo fmt
jobs:
  cargofmt:
    name: cargo fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Use correct Rust toolchain
        shell: bash
        run: |
          [ -e rust-toolchain.toml ] || cp rust/rust-toolchain.toml ./
      - name: Install rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          cache-workspaces: rust
          components: rustfmt

      - name: Run cargo fmt
        run: |
          cd rust
          cargo fmt -- --check
        shell: bash
