name: CI

on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main
    paths:
      - '.github/workflows/ci.yaml'
      - '**/*.rs'
      - '**/Cargo.toml'
      - '**/Cargo.lock'
      - 'data/**'
  workflow_dispatch:

concurrency:
  group: ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: -D warnings

jobs:
  format:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Check formatting
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - name: Run Clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Run tests (all features)
        run: cargo test --workspace --all-features --locked --verbose

  no-std:
    name: Build no_std
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Build without default features
        run: cargo build --no-default-features
      - name: Build without default features (all optional integrations)
        run: cargo build --no-default-features --features serde,schemars,arbitrary,proptest

  msrv:
    name: MSRV (1.93.0)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@1.93.0
      - uses: Swatinem/rust-cache@v2
      - name: Build on MSRV
        run: cargo build --locked --all-features

  cfi-table:
    name: CFI table in sync
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Regenerate CFI table
        run: cargo run --bin generate_cfi_table --features codegen
      - name: Format generated table
        run: cargo fmt
      - name: Fail if the committed table drifted
        run: git diff --exit-code -- src/cfi/table.rs

  docs:
    name: Docs
    runs-on: ubuntu-latest
    env:
      RUSTDOCFLAGS: -D warnings
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@stable
      - name: Build documentation
        run: cargo doc --all-features --no-deps

  fuzz:
    name: Fuzz build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      # cargo-fuzz requires a nightly toolchain.
      - uses: dtolnay/rust-toolchain@nightly
      - uses: Swatinem/rust-cache@v2
        with:
          # The fuzz crate detaches from the workspace; cache it separately.
          workspaces: fuzz
      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz --locked
      # Compile-check every fuzz target. Deterministic and fast; catches API
      # or target breakage without a nondeterministic bounded run.
      - name: Build fuzz targets
        run: cargo +nightly fuzz build
