name: ci

env:
  RUST_VERSION: 1.91.1

on:
  pull_request:
    branches: [master]
    types: [opened, synchronize, reopened, ready_for_review]
  workflow_dispatch: {}

concurrency:
  group: currencylib-ci
  cancel-in-progress: false

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    if: github.event.pull_request.draft == false
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: TruffleHog Leaks Scan
        uses: trufflesecurity/trufflehog@v3.91.2
        with:
          extra_args: --only-verified

      - name: Cache run data
        id: ci-cache-check
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ~/.cargo/bin
            ~/.rustup
            target
          key: ${{ runner.os }}-ci-cache-check-${{ hashFiles('**/Cargo.lock') }}

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ env.RUST_VERSION }}
          override: true

      - name: Install clippy
        if: steps.ci-cache-check.outputs.cache-hit != 'true'
        run: rustup component add clippy --toolchain ${{ env.RUST_VERSION }}

      - name: Run cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --all-features

      - name: Run cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: --all-features

      - name: Run cargo doc
        uses: actions-rs/cargo@v1
        with:
          command: doc
          args: --all-features

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: check
    steps:
      - name: Checkout codebase
        uses: actions/checkout@v6

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

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: ${{ env.RUST_VERSION }}
          override: true

      - name: Install cargo-llvm-cov
        run: which cargo-llvm-cov || cargo install cargo-llvm-cov --locked

      - name: Run tests and generate lcov
        run: make lcov

      - name: Upload lcov artifact
        uses: actions/upload-artifact@v4
        with:
          name: lcov-info
          path: target/coverage/lcov.info
          if-no-files-found: error

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    needs: test
    permissions:
      contents: read
    steps:
      - name: Checkout codebase
        uses: actions/checkout@v6

      - name: Download lcov artifact
        uses: actions/download-artifact@v4
        with:
          name: lcov-info
          path: target/coverage

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./target/coverage/lcov.info
          fail_ci_if_error: true
