name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: -Dwarnings

jobs:
  fmt:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --all --check

  clippy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --all-features

  test:
    needs: [fmt, clippy]
    strategy:
      matrix:
        target:
          - os: ubuntu-latest
            triple: x86_64-unknown-linux-musl
            profile: release
          - os: macos-latest
            triple: aarch64-apple-darwin
            profile: release
          - os: windows-latest
            triple: x86_64-pc-windows-msvc
            profile: debug
    runs-on: ${{ matrix.target.os }}
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target.triple }}
      - name: Install musl tools
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y musl-tools
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ${{ runner.os }}
      - name: cargo test
        run: |
          if [ "${{ matrix.target.profile }}" = "release" ]; then
            cargo test --release --target ${{ matrix.target.triple }}
          else
            cargo test --target ${{ matrix.target.triple }}
          fi
        shell: bash

  build:
    needs: [fmt, clippy]
    strategy:
      matrix:
        target:
          - os: ubuntu-latest
            triple: x86_64-unknown-linux-musl
            profile: release
            ext: ""
          - os: ubuntu-latest
            triple: aarch64-unknown-linux-musl
            profile: release
            ext: ""
          - os: macos-latest
            triple: x86_64-apple-darwin
            profile: release
            ext: ""
          - os: macos-latest
            triple: aarch64-apple-darwin
            profile: release
            ext: ""
          - os: windows-latest
            triple: x86_64-pc-windows-msvc
            profile: debug
            ext: ".exe"
    runs-on: ${{ matrix.target.os }}
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target.triple }}
      - name: Install musl tools
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y musl-tools
      - name: Install aarch64 musl toolchain
        if: matrix.target.triple == 'aarch64-unknown-linux-musl'
        run: |
          curl -sSL https://github.com/cross-tools/musl-cross/releases/latest/download/aarch64-unknown-linux-musl.tar.xz | tar -xJ -C /tmp
          echo "/tmp/aarch64-unknown-linux-musl/bin" >> $GITHUB_PATH
      - name: Set cross-compile env
        if: matrix.target.triple == 'aarch64-unknown-linux-musl'
        run: |
          echo "CC_aarch64_unknown_linux_musl=aarch64-unknown-linux-musl-gcc" >> $GITHUB_ENV
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER=aarch64-unknown-linux-musl-gcc" >> $GITHUB_ENV
      - uses: Swatinem/rust-cache@v2
        with:
          shared-key: ${{ runner.os }}

      - name: cargo build
        run: |
          if [ "${{ matrix.target.profile }}" = "release" ]; then
            cargo build --release --target ${{ matrix.target.triple }}
          else
            cargo build --target ${{ matrix.target.triple }}
          fi
        shell: bash

