name: CI

on:
  push:
    branches: [master]
  pull_request:

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test & lint (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v6

      - name: Fetch the intl crate (path-dependency sibling)
        run: git clone --depth 1 https://github.com/KarpelesLab/intlrs.git ../unicoders

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - name: Cache
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.os }}

      - name: Format
        if: runner.os == 'Linux' # formatting is platform-independent
        # Scope to our own package: the `intl` path dependency (`../unicoders`) is
        # an upstream checkout whose formatting we do not gate on.
        run: cargo fmt --check -p kataan

      - name: Clippy (all features, warnings denied)
        run: cargo clippy --all-targets --all-features -- -D warnings

      - name: Test (all features)
        run: cargo test --all-features

  core_builds:
    name: Core / no_std builds
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Fetch the intl crate (path-dependency sibling)
        run: git clone --depth 1 https://github.com/KarpelesLab/intlrs.git ../unicoders

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      # The pure language core must build with only `alloc` — no std, no host.
      - name: Build language core (alloc only)
        run: cargo build --no-default-features --features alloc

      - name: Build with regex but no host
        run: cargo build --no-default-features --features alloc,regex,intl

  c_abi:
    name: C ABI smoke test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: Fetch the intl crate (path-dependency sibling)
        run: git clone --depth 1 https://github.com/KarpelesLab/intlrs.git ../unicoders

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: Build the C library (static + shared)
        run: |
          cargo rustc --lib --release --features ffi --crate-type staticlib
          cargo rustc --lib --release --features ffi --crate-type cdylib

      - name: Compile and run the C smoke test
        run: |
          cc tests/ffi_smoke.c -I include target/release/libkataan.a \
             -lpthread -ldl -lm -o ffi_smoke
          ./ffi_smoke

  docs:
    name: Docs build (warnings denied)
    runs-on: ubuntu-latest
    env:
      RUSTDOCFLAGS: -D warnings -D rustdoc::broken-intra-doc-links
    steps:
      - uses: actions/checkout@v6

      - name: Fetch the intl crate (path-dependency sibling)
        run: git clone --depth 1 https://github.com/KarpelesLab/intlrs.git ../unicoders

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Cache
        uses: Swatinem/rust-cache@v2

      - name: cargo doc --no-deps --all-features
        run: cargo doc --no-deps --all-features
