name: ci

on:
  push:
    branches:
      - main
      - "feat/**"
      - "fix/**"
      - "docs/**"
      - "chore/**"
      - "test/**"
      - "refactor/**"
      - "ci/**"
      - "build/**"
      - "perf/**"
      - "style/**"
  pull_request:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  arch-pkgbuild:
    name: Arch PKGBUILD
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Validate PKGBUILD shell syntax
        run: bash -n arch/PKGBUILD

  fmt:
    name: Format
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup toolchain install stable --profile minimal --component rustfmt
          rustup default stable

      - name: Check formatting
        working-directory: pixie
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup toolchain install stable --profile minimal --component clippy
          rustup default stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            pixie/target
          key: ${{ runner.os }}-cargo-clippy-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-clippy-
            ${{ runner.os }}-cargo-

      - name: Run clippy
        working-directory: pixie
        run: cargo clippy --locked --all-targets --all-features -- -D warnings

  test:
    name: Tests
    runs-on: ubuntu-latest
    needs:
      - arch-pkgbuild
      - fmt
      - clippy

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup toolchain install stable --profile minimal
          rustup default stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            pixie/target
          key: ${{ runner.os }}-cargo-test-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-test-
            ${{ runner.os }}-cargo-

      - name: Run tests
        working-directory: pixie
        run: cargo test --all-features --locked --verbose

  build:
    name: Build release
    runs-on: ubuntu-latest
    needs:
      - test

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup toolchain install stable --profile minimal
          rustup default stable

      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            pixie/target
          key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-build-
            ${{ runner.os }}-cargo-

      - name: Build release
        working-directory: pixie
        run: cargo build --release --locked --verbose
