name: master
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: read

jobs:
  changed-files:
    runs-on: ubuntu-latest
    outputs:
      any_rs_changed: ${{ steps.changed-files.outputs.any_changed }}
    steps:
      - uses: actions/checkout@v4
      - uses: tj-actions/changed-files@v46
        id: changed-files
        with:
          files: |
            {src,tests}/**/*.rs
            Cargo.toml
            Cargo.lock

  _test:
    needs: changed-files
    if: needs.changed-files.outputs.any_rs_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: sudo apt-get install -y ffmpeg
      - uses: taiki-e/install-action@nextest
      - run: cargo nextest run

  test:
    needs: _test
    if: ${{ !(failure() || cancelled()) }}
    runs-on: ubuntu-latest
    steps:
      - name: Tests OK (passed or skipped)
        run: exit 0

  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - uses: jdx/mise-action@v2
        with:
          mise_toml: |
            [tasks]
            "check-format" = { run = "dprint check --excludes mise.toml" }
            "check-rustfmt" = { run = "cargo fmt -- --check" }
            "check-clippy" = { run = "cargo clippy -- -D warnings" }
            "check" = { depends = "check-*" }
            [tools]
            dprint = "latest"
      - run: mise r check
