name: Rust

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

env:
  CARGO_TERM_COLOR: always

jobs:
  cargo-fmt-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Check formatting
      run: cargo fmt --check

  build-native:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v3
    - uses: Swatinem/rust-cache@v2
    - name: Set up Python 3.10
      uses: actions/setup-python@v5
      with:
        python-version: "3.10"
    - name: Build
      run: cargo build --verbose
    - name: Test
      env:
        RUST_LOG: ${{ runner.debug && 'limbo_core::storage=trace' || '' }}
      run: cargo test --verbose
      timeout-minutes: 10


  clippy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Clippy
        run: |
          cargo clippy --workspace --all-features --all-targets --exclude limbo-wasm --exclude sqlite3-parser -- -A clippy::all -W clippy::correctness -W clippy::perf -W clippy::suspicious --deny=warnings
      - name: Clippy `limbo-wasm` crate `nodejs` feature
        run: |
          cargo clippy --package limbo-wasm --features nodejs --all-targets --no-deps -- -A clippy::all -W clippy::correctness -W clippy::perf -W clippy::suspicious --deny=warnings
      - name: Clippy `limbo-wasm` crate `web` feature
        run: |
          cargo clippy --package limbo-wasm --no-default-features --features web --all-targets --no-deps -- -A clippy::all -W clippy::correctness -W clippy::perf -W clippy::suspicious --deny=warnings

  build-wasm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
      - run: wasm-pack build --target nodejs bindings/wasm

  bench:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Bench
        run: cargo bench

  test-limbo:
    runs-on: ubuntu-latest
    steps:
      - name: Install cargo-c
        env:
          LINK: https://github.com/lu-zero/cargo-c/releases/download/v0.10.7
          CARGO_C_FILE: cargo-c-x86_64-unknown-linux-musl.tar.gz
        run: |
          curl -L $LINK/$CARGO_C_FILE | tar xz -C ~/.cargo/bin

      - uses: actions/checkout@v3
      - uses: "./.github/shared/install_sqlite"
      - name: Test
        run: make test

  test-sqlite:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: "./.github/shared/install_sqlite"
      - name: Test
        run: SQLITE_EXEC="sqlite3" make test-compat
