shvclient 5.0.1

A Rust framework for Silicon Heaven RPC devices
name: Rust

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-Dwarnings"

permissions:
  # For syyyr/rust-pycobertura-action
  pull-requests: write

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        feature: ["tokio", "smol"]
        toolchain: [stable, nightly]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Install system dependencies
        run: |
          sudo apt update
          sudo apt-get install -y libudev-dev pkg-config
      - name: Set up Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: clippy
          toolchain: ${{ matrix.toolchain }}
          override: true
      - name: Clippy (${{ matrix.feature }})
        run: cargo clippy --all-targets --features ${{ matrix.feature }}
      - name: Build (${{ matrix.feature }})
        run: cargo build --all-targets --features ${{ matrix.feature }}
      - name: Run tests (${{ matrix.feature }})
        run: cargo test --all-targets --features ${{ matrix.feature }}

  coverage:
    name: Code coverage
    runs-on: ubuntu-latest
    steps:
      - name: Install system dependencies
        run: |
          sudo apt update
          sudo apt-get install -y libudev-dev pkg-config
      - name: Generate coverage
        uses: silicon-heaven/rust-pycobertura-action@v4.1.0
        with:
          project_name: libshvrpc
          cargo_test_arguments: --features smol

  check-version-bump:
    name: Check version bump
    runs-on: ubuntu-latest
    steps:
      - name: Check version bump
        id: version-check
        continue-on-error: ${{startsWith(github.head_ref, 'dependabot/')}}
        uses: silicon-heaven/rust-check-version-bump@v1.0.1

      - name: Checkout code
        if: steps.version-check.outcome == 'failure'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          ref: ${{github.head_ref}}

      - name: Bump version
        if: steps.version-check.outcome == 'failure'
        run: |
          [[ "$(grep '^version' Cargo.toml | head -n1)" =~ ([0-9]+\.[0-9]+\.)([0-9]+) ]]
          NEW_VERSION="${BASH_REMATCH[1]}$((BASH_REMATCH[2] + 1))"
          cargo install cargo-bump
          echo Bumping "${BASH_REMATCH[0]} -> $NEW_VERSION"
          if git show | grep cargo-shv-major; then
            echo Detected shvproto/shvrpc major version update, bumping by major.
            BUMP_TYPE='major'
          else
            BUMP_TYPE='patch'
          fi
          cargo bump "$BUMP_TYPE"
          if [[ -f Cargo.lock ]]; then
              cargo update
          fi
          git diff
          git config --global user.name "Login will be determined by the Github API based on the creator of the token"
          git config --global user.email ""
          git commit -am '[dependabot-skip] Bump version'

      - name: Push changes
        if: steps.version-check.outcome == 'failure'
        uses: Asana/push-signed-commits@v1
        with:
          github-token: ${{ secrets.DEPENDABOT_GITHUB_TOKEN }}
          local_branch_name: ${{github.head_ref}}
          remote_branch_name: ${{github.head_ref}}

  tag-release:
    name: Tag Release
    if: github.ref == 'refs/heads/main'
    needs: [build, check-version-bump]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Get version from Cargo.toml
        id: get-version
        run: |
          version="$(grep '^version' Cargo.toml | head -n1 | sed 's/version\s*=\s*"\(.*\)"/\1/')"
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Create and push tag
        uses: rickstaa/action-create-tag@v1
        with:
          tag: ${{ steps.get-version.outputs.version }}