name: Rust

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

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

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-Dwarnings"

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

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest]
        toolchain: [stable, nightly]
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            features: '--all-features'
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            features: '--features websocket'
    runs-on: ${{ matrix.os }}
    steps:
    - uses: actions/checkout@v7
    - name: Set up Rust
      uses: actions-rust-lang/setup-rust-toolchain@v1
      with:
        target: ${{ matrix.target }}
        components: clippy
        toolchain: ${{ matrix.toolchain }}
        override: true
    - name: Clippy
      run: cargo clippy --verbose --all-targets ${{matrix.features}}
    - name: Build
      run: cargo build --verbose  ${{matrix.features}}
    - name: Run tests
      run: cargo test --verbose  ${{matrix.features}}

  coverage:
    name: Code coverage
    runs-on: ubuntu-latest
    steps:
      - name: Generate coverage
        uses: silicon-heaven/rust-pycobertura-action@v4.1.0
        with:
          project_name: libshvrpc

  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@v7
        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/master'
    needs: [build, check-version-bump]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v7
        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 }}
