name: ci

on:
  push:
    branches: [main]
  pull_request:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

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

jobs:
  rust:
    name: Rust (fmt, clippy, test)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - uses: Swatinem/rust-cache@v2

      - name: rustfmt
        run: cargo fmt --all --check

      - name: clippy
        run: cargo clippy --workspace --all-targets --all-features -- -D warnings

      - name: test
        run: cargo test --workspace --all-features

  node:
    name: Node (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
          cache-dependency-path: crates/eregex-node/package-lock.json

      - name: Install dependencies
        working-directory: crates/eregex-node
        run: npm ci

      - name: Build native addon
        working-directory: crates/eregex-node
        run: npm run build

      - name: Test
        working-directory: crates/eregex-node
        run: npm test

  python:
    name: Python (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Build wheel
        uses: PyO3/maturin-action@v1
        with:
          command: build
          args: --release --manifest-path crates/eregex-python/Cargo.toml --out dist

      - name: Install wheel
        run: pip install --no-index --find-links dist eregex

      - name: Test
        working-directory: crates/eregex-python
        run: python -m unittest test_eregex -v

  wasm:
    name: WASM (nodejs target)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown

      - uses: Swatinem/rust-cache@v2

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install wasm-pack
        run: cargo install wasm-pack --locked

      - name: Build WASM package
        working-directory: crates/eregex-wasm
        run: npm run build

      - name: Test
        working-directory: crates/eregex-wasm
        run: npm test
