name: Rust

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

env:
  CARGO_TERM_COLOR: always
  PROTOC_VERSION: 3.23.4
  # FIXME: There are some warnings depending on certain feature flags that
  # we need to fix before we can enable this.
  # RUSTFLAGS: "-D warnings"

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  checks:
    runs-on: ubuntu-latest
    name: Run Checks
    env:
      RUSTFLAGS: -D warnings --cfg tokio_unstable
    steps:
      - uses: hecrj/setup-rust-action@v2

      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive

      - name: Set up cargo cache
        uses: actions/cache@v3
        continue-on-error: false
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-

      - name: Cargo fmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --check

      - name: Cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --all-targets --all-features

      - name: Test openssl
        shell: bash {0}
        run: cargo tree -p libsql-server -i openssl; [ $? = 101 ]

      # Disabled because we vendor rusqlite which does not use
      # clippy so it warns.
      # - name: Clippy
      #   uses: actions-rs/clippy-check@v1
      #   with:
      #     token: ${{ secrets.GITHUB_TOKEN }}
      #     args: --all-targets --all-features -- -D warnings


  features:
    name: Check features and unused dependencies
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: dtolnay/rust-toolchain@nightly
      - uses: taiki-e/install-action@cargo-hack
      - uses: taiki-e/install-action@cargo-udeps
      - uses: Swatinem/rust-cache@v2
      - run: cargo +nightly hack udeps -p libsql --each-feature
      - run: RUSTFLAGS="-D warnings --cfg tokio_unstable" cargo check -p libsql --no-default-features --features core 
      - run: RUSTFLAGS="-D warnings --cfg tokio_unstable" cargo check -p libsql --no-default-features --features replication 
      - run: RUSTFLAGS="-D warnings --cfg tokio_unstable" cargo check -p libsql --no-default-features --features remote

  test:
    runs-on: ubuntu-latest
    name: Run Tests
    env:
      RUSTFLAGS: -D warnings --cfg tokio_unstable
    steps:
    - uses: hecrj/setup-rust-action@v2

    # needed because we run out of disk space during tests
    - name: Free Disk Space (Ubuntu)
      uses: jlumbroso/free-disk-space@main
      with:
        # this might remove tools that are actually needed,
        # when set to "true" but frees about 6 GB
        tool-cache: true

    - name: Install deps
      run: sudo apt update && sudo apt install -y libclang-dev

    - name: Install protoc
      uses: taiki-e/install-action@v2
      with:
        tool: protoc@${{ env.PROTOC_VERSION }}

    - uses: taiki-e/install-action@v2
      with:
        tool: nextest

    - uses: actions/checkout@v3

    - name: Set up cargo cache
      uses: actions/cache@v3
      continue-on-error: false
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: ${{ runner.os }}-cargo-

    - name: Run tests
      run: cargo nextest run
  # test-custom-pager:
  #   runs-on: ubuntu-latest
  #   name: Run Tests
  #   env:
  #     RUSTFLAGS: -D warnings --cfg tokio_unstable
  #   steps:
  #   - uses: hecrj/setup-rust-action@v2

  #   # needed because we run out of disk space during tests
  #   - name: Free Disk Space (Ubuntu)
  #     uses: jlumbroso/free-disk-space@main
  #     with:
  #       # this might remove tools that are actually needed,
  #       # when set to "true" but frees about 6 GB
  #       tool-cache: true

  #   - name: Install deps
  #     run: sudo apt update && sudo apt install -y libclang-dev

  #   - name: Install protoc
  #     uses: taiki-e/install-action@v2
  #     with:
  #       tool: protoc@${{ env.PROTOC_VERSION }}

  #   - uses: taiki-e/install-action@v2
  #     with:
  #       tool: nextest

  #   - uses: actions/checkout@v3

  #   - name: Set up cargo cache
  #     uses: actions/cache@v3
  #     continue-on-error: false
  #     with:
  #       path: |
  #         ~/.cargo/bin/
  #         ~/.cargo/registry/index/
  #         ~/.cargo/registry/cache/
  #         ~/.cargo/git/db/
  #         target/
  #       key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
  #       restore-keys: ${{ runner.os }}-cargo-
  #   - name: Run tests with custom pager
  #     env:
  #       LIBSQL_EXPERIMENTAL_PAGER: 1000
  #     run: cargo nextest run
  test-encryption:
    runs-on: ubuntu-latest
    name: Run Tests
    env:
      RUSTFLAGS: -D warnings --cfg tokio_unstable
    steps:
    - uses: hecrj/setup-rust-action@v2

    # needed because we run out of disk space during tests
    - name: Free Disk Space (Ubuntu)
      uses: jlumbroso/free-disk-space@main
      with:
        # this might remove tools that are actually needed,
        # when set to "true" but frees about 6 GB
        tool-cache: true

    - name: Install deps
      run: sudo apt update && sudo apt install -y libclang-dev

    - name: Install protoc
      uses: taiki-e/install-action@v2
      with:
        tool: protoc@${{ env.PROTOC_VERSION }}

    - uses: taiki-e/install-action@v2
      with:
        tool: nextest

    - uses: actions/checkout@v3

    - name: Set up cargo cache
      uses: actions/cache@v3
      continue-on-error: false
      with:
        path: |
          ~/.cargo/bin/
          ~/.cargo/registry/index/
          ~/.cargo/registry/cache/
          ~/.cargo/git/db/
          target/
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: ${{ runner.os }}-cargo-
    - name: embedded replica encryption tests
      run: cargo xtask test-encryption
  windows:
    runs-on: windows-latest
    name: Windows checks
    steps:
      - uses: hecrj/setup-rust-action@v2
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Set up cargo cache
        uses: actions/cache@v3
        continue-on-error: false
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-
      - name: build libsql all features
        run: cargo build -p libsql --all-features 

  # test-rust-wasm:
  #   runs-on: ubuntu-latest
  #   name: Run Rust Wasm Tests
  #   env:
  #     RUSTFLAGS: -D warnings
  #   steps:
  #     - uses: hecrj/setup-rust-action@v2
  #       with:
  #         targets: [wasm32-unknown-unknown]

  #     # needed because we run out of disk space during tests
  #     - name: Free Disk Space (Ubuntu)
  #       uses: jlumbroso/free-disk-space@main
  #       with:
  #         # this might remove tools that are actually needed,
  #         # when set to "true" but frees about 6 GB
  #         tool-cache: true

  #     - uses: actions/checkout@v3

  #     - name: Set up cargo cache
  #       uses: actions/cache@v3
  #       continue-on-error: false
  #       with:
  #         path: |
  #           ~/.cargo/bin/
  #           ~/.cargo/registry/index/
  #           ~/.cargo/registry/cache/
  #           ~/.cargo/git/db/
  #           target/
  #         key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
  #         restore-keys: ${{ runner.os }}-cargo-

  #     - name: Run check
  #       run: cargo check --verbose -p libsql --target wasm32-unknown-unknown --no-default-features --features cloudflare