name: Check Rust

on:
  push:
    paths-ignore:
      - "doc/**"
  pull_request:
    paths-ignore:
      - "doc/**"

permissions:
  contents: read #  to fetch code (actions/checkout)

jobs:
  check-rust:
    name: Check Rust
    runs-on: ubuntu-latest
    container: almalinux:9
    steps:
      - name: Cache rust
        uses: actions/cache@v3.3.1
        with:
          path: ~/.cargo
          key: check-rust

      - name: Install system packages
        run: |
          dnf -y install dnf-plugins-core
          dnf config-manager --set-enabled crb
          dnf -y install \
                autoconf \
                automake \
                cargo-vendor \
                diffutils \
                numactl-devel \
                dpdk-devel \
                file-devel \
                gcc \
                gcc-c++ \
                git \
                jansson-devel \
                jq \
                lua-devel \
                libtool \
                libyaml-devel \
                libnfnetlink-devel \
                libnetfilter_queue-devel \
                libnet-devel \
                libcap-ng-devel \
                libevent-devel \
                libmaxminddb-devel \
                libpcap-devel \
                libtool \
                lz4-devel \
                make \
                nss-devel \
                pcre2-devel \
                pkgconfig \
                python3-devel \
                python3-sphinx \
                python3-yaml \
                sudo \
                which \
                zlib-devel

      - name: Installing Rust
        run: |
          curl https://sh.rustup.rs -sSf | sh -s -- -y
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH
      - name: Install cbindgen
        run: cargo install --debug cbindgen
      - uses: actions/checkout@v3.5.3
      - run: git config --global --add safe.directory /__w/suricata/suricata
      - run: ./scripts/bundle.sh
      - run: ./autogen.sh
      - run: ./configure
      - run: cargo clippy --all-features --fix --allow-no-vcs
        working-directory: rust
      - run: |
          diff=$(git diff)
          if [ "${diff}" ]; then
              echo "${diff}"
              echo "::error ::Clippy --fix made changes, please fix"
              exit 1
          fi
      - run: cargo clippy --all-features
        working-directory: rust
