name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      - uses: Swatinem/rust-cache@v2
      - name: Clippy
        run: cargo clippy -- -D warnings
      - name: Rustfmt
        run: cargo fmt --all --check

  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Build
        run: cargo build --verbose
      - name: Run tests
        run: cargo test --verbose

  coverage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-tarpaulin
      - name: Coverage Report
        # Tarpaulin's --timeout is the per-test watchdog. COS-pricer
        # calibration tests (fx_hlmm_calibrator, eurusd_worst_case
        # smile-fit) can take ~60s locally under coverage
        # instrumentation; bump to 300 s so they aren't killed on
        # Actions runners which are slower than dev boxes.
        run: |
          cargo tarpaulin \
          --verbose \
          --all-features \
          --workspace \
          --timeout 300 \
          --out Xml
      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v3
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
