# Copyright © 2025-26 l5yth & contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Rust

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
    paths:
      - '.github/**'
      - 'matrix/**'
      - 'tests/**'

permissions:
  contents: read

jobs:
  matrix:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v5
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
      - name: Cache Cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            ./matrix/target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml', '**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-
      - name: Show rustc version
        run: rustc --version
      - name: Install llvm-tools-preview component
        run: rustup component add llvm-tools-preview --toolchain stable
      - name: Install cargo-llvm-cov
        working-directory: ./matrix
        run: cargo install cargo-llvm-cov --locked
      - name: Check formatting
        working-directory: ./matrix
        run: cargo fmt --all -- --check
      - name: Clippy lint
        working-directory: ./matrix
        run: cargo clippy --all-targets --all-features -- -D warnings
      - name: Build
        working-directory: ./matrix
        run: cargo build --all --all-features
      - name: Test
        working-directory: ./matrix
        run: cargo test --all --all-features --verbose
      - name: Run tests with coverage
        working-directory: ./matrix
        run: |
          cargo llvm-cov --all-features --workspace --lcov --output-path coverage.lcov
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./matrix/coverage.lcov
          flags: matrix-bridge
          name: matrix-bridge
          fail_ci_if_error: false
