name: Check

on:
  push: {}
  pull_request: {}

jobs:
  clippy:
    name: Clippy (${{ matrix.name }})
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Linux
            os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - name: macOS
            os: macos-14
            target: aarch64-apple-darwin
          - name: iOS
            os: macos-14
            target: aarch64-apple-ios

    steps:
      - uses: actions/checkout@v4

      - name: Prepare (ubuntu)
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install librust-alsa-sys-dev libglib2.0-dev libasound2t64 libatk-adaptor libgail-common libgtk-3-dev

      - name: Setup Rust toolchain
        run: |
          rustup toolchain install
          rustup target add ${{ matrix.target }}

      - name: Select Xcode (iOS)
        if: matrix.target == 'aarch64-apple-ios'
        run: |
          sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
          xcodebuild -version
          xcrun --sdk iphoneos --show-sdk-path

      - name: Cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-on-failure: "true"
          cache-all-crates: "true"

      - name: Clippy
        run: |
          cargo clippy --all-targets --all-features --workspace \
            --target ${{ matrix.target }} -- \
            -D warnings

  fmt:
    name: Format
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@v4

      - name: Prepare
        run: |
          sudo apt-get update
          sudo apt-get install librust-alsa-sys-dev libglib2.0-dev libasound2t64 libatk-adaptor libgail-common libgtk-3-dev

      - name: Setup Rust toolchain
        run: rustup toolchain install

      - name: Cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-on-failure: "true"
          cache-all-crates: "true"

      - name: Format
        run: cargo fmt --all --check

  tests:
    name: Tests
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@v4

      - name: Prepare
        run: |
          sudo apt-get update
          sudo apt-get install librust-alsa-sys-dev libglib2.0-dev libasound2t64 libatk-adaptor libgail-common libgtk-3-dev

      - name: Setup Rust toolchain
        run: rustup toolchain install

      - name: Cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-on-failure: "true"
          cache-all-crates: "true"

      - name: Run tests
        run: cargo test --workspace --no-fail-fast
