name: Build and test

on:
  push:
    branches:
      - "**"
  pull_request:

env:
  RUST_BACKTRACE: 1
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        flags: [--all-features, --no-default-features]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: install libusb-1.0
        run: sudo apt-get install libusb-1.0-0-dev
        if: matrix.os == 'ubuntu-latest'
      - name: install llvm and clang
        uses: KyleMayes/install-llvm-action@v1
        with:
          version: "10.0"
          directory: ${{ runner.temp }}/llvm
        if: matrix.os == 'windows-latest'
      - name: set LIBCLANG_PATH
        run: echo "LIBCLANG_PATH=$((gcm clang).source -replace "clang.exe")" >> $env:GITHUB_ENV
        if: matrix.os == 'windows-latest'

      - uses: dtolnay/rust-toolchain@stable
      - run: cargo build ${{ matrix.flags }} --release
      - run: cargo build ${{ matrix.flags }} --release --examples

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        flags: [--all-features, --no-default-features]
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: install libusb-1.0
        run: sudo apt-get install libusb-1.0-0-dev
        if: matrix.os == 'ubuntu-latest'
      - name: install llvm and clang
        uses: KyleMayes/install-llvm-action@v1
        with:
          version: "10.0"
          directory: ${{ runner.temp }}/llvm
        if: matrix.os == 'windows-latest'
      - name: set LIBCLANG_PATH
        run: echo "LIBCLANG_PATH=$((gcm clang).source -replace "clang.exe")" >> $env:GITHUB_ENV
        if: matrix.os == 'windows-latest'

      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test ${{ matrix.flags }} --all

  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: 'true'
      - name: install libusb-1.0
        run: sudo apt-get install libusb-1.0-0-dev
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - run: cargo fmt -- --check
      - run: cargo clippy --all-targets --all-features