name: CI

on:
  push:
    branches:
      - master
      - dev
  pull_request:
  workflow_dispatch:

jobs:
  check:
    name: Check
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6
      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo build
        uses: actions/cache@v5
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rust
        run: rustup update stable
      - name: Install libheif
        run: |
          sudo add-apt-repository -y ppa:strukturag/libheif
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends libheif-dev libheif1 libsharpyuv-dev
      - name: Run check
        run: cargo check

  test:
    name: Test Suite
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6
      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo build
        uses: actions/cache@v5
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rust
        run: rustup update stable
      - name: Install libheif
        run: |
          sudo add-apt-repository -y ppa:strukturag/libheif
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends libheif-dev libheif1 libsharpyuv-dev
      - name: Install just
        uses: extractions/setup-just@v3
      - name: Run tests
        run: just test

  fmt:
    name: Rustfmt
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6
      - name: Install Rust
        run: rustup update stable
      - name: Install rustfmt
        run: rustup component add rustfmt
      - name: Install just
        uses: extractions/setup-just@v3
      - name: Run rustfmt
        run: just check-format

  clippy:
    name: Clippy
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6
      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo build
        uses: actions/cache@v5
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rust
        run: rustup update stable
      - name: Install libheif
        run: |
          sudo add-apt-repository -y ppa:strukturag/libheif
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends libheif-dev libheif1 libsharpyuv-dev
      - name: Install clippy
        run: rustup component add clippy
      - name: Install just
        uses: extractions/setup-just@v3
      - name: Run clippy
        run: just lint

  coverage:
    name: Coverage
    runs-on: ubuntu-24.04
    env:
      CARGO_TERM_COLOR: always
    steps:
      - uses: actions/checkout@v6
      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Cache cargo build
        uses: actions/cache@v5
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
      - name: Install Rust
        run: rustup update stable
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Install libheif
        run: |
          sudo add-apt-repository -y ppa:strukturag/libheif
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends libheif-dev libheif1 libsharpyuv-dev
      - name: Install just
        uses: extractions/setup-just@v3
      - name: Generate code coverage
        run: just coverage
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: lcov.info
          fail_ci_if_error: false
