name: Linters
on: [workflow_call]
permissions: {}

jobs:
  uniffi-bindings:
      runs-on: ubuntu-22.04
      container:
          image: ghcr.io/nordsecurity/uniffi-generators:v0.25.0-8
      steps:
        - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
        - run: uniffi-bindgen generate src/libtelio.udl --language python
        - run: mkdir -p dist/bindings/
        - run: cp src/telio.py dist/bindings/telio_bindings.py
        - uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # v3.1.2
          with:
            name: telio_bindings.py
            path: dist/bindings
  check:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions-rs/cargo@ae10961054e4aa8b4aa7dffede299aaf087aa33b # v1.0.3
          with:
            command: check
            args: --target "x86_64-unknown-linux-gnu" --lib --color always
  clippy:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions-rs/toolchain@b2417cde72dcf67f306c0ae8e0828a81bf0b189f # v1.0.6
          with:
            components: clippy
        - uses: actions-rs/cargo@ae10961054e4aa8b4aa7dffede299aaf087aa33b # v1.0.3
          with:
            command: clippy
            args: --target "x86_64-unknown-linux-gnu" --lib --color always -- --deny warnings --allow unknown-lints -W clippy::expect_used -W clippy::panic -W clippy::unwrap_used
  clippy-win:
      runs-on: windows-2022
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions-rs/toolchain@b2417cde72dcf67f306c0ae8e0828a81bf0b189f # v1.0.6
          with:
            target: x86_64-pc-windows-gnu
            components: clippy
        - uses: actions-rs/cargo@ae10961054e4aa8b4aa7dffede299aaf087aa33b # v1.0.3
          with:
            command: clippy
            args: --target "x86_64-pc-windows-gnu" --lib --color always -- --deny warnings --allow unknown-lints -W clippy::expect_used -W clippy::panic -W clippy::unwrap_used
  clippy-mac:
      runs-on: macos-13
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - run: cargo clippy --verbose --target "x86_64-apple-darwin" --lib --color always -- --deny warnings --allow unknown-lints -W clippy::expect_used -W clippy::panic -W clippy::unwrap_used
  deny:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: EmbarkStudios/cargo-deny-action@3f4a782664881cf5725d0ffd23969fcce89fd868 # v1.6.3
          with:
            rust-version: 1.77.2 # NOTE: Runs in its own docker container and doesn't support toolchain files
  udeps:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions-rs/toolchain@b2417cde72dcf67f306c0ae8e0828a81bf0b189f # v1.0.6
          with:
            toolchain: nightly-2024-05-10
            override: true
        - uses: aig787/cargo-udeps-action@1cd634a329e14ccfbccfe7c96497d14dac24a743 # v1
          with:
            version: 'v0.1.47'
            args: '--workspace --locked --output human --backend depinfo'
  rustfmt:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions-rs/toolchain@b2417cde72dcf67f306c0ae8e0828a81bf0b189f # v1.0.6
          with:
            components: rustfmt
        - uses: actions-rs/cargo@ae10961054e4aa8b4aa7dffede299aaf087aa33b # v1.0.3
          with:
            command: fmt
            args: -- --check --color always
  python-format-black:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - run: pip3 install --no-deps -r requirements.txt
        - run: pipenv install --system
        - run: pipenv install --system
          working-directory: nat-lab
        - run: black --check --diff --color .
          working-directory: nat-lab
        - run: black --check --diff --color .
          working-directory: ci
  python-format-isort:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - run: pip3 install --no-deps -r requirements.txt
        - run: pipenv install --system
        - run: isort --check-only --diff .
          working-directory: nat-lab
  python-format-autoflake:
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - run: pip3 install --no-deps -r requirements.txt
        - run: pipenv install --system
        - run: autoflake --check .
          working-directory: nat-lab
  python-lint:
      needs: uniffi-bindings
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
          with:
            name: telio_bindings.py
            path: dist/bindings
        - run: cp dist/bindings/telio_bindings.py nat-lab/tests/uniffi/telio_bindings.py
        - run: pip3 install --no-deps -r requirements.txt
        - run: pipenv install --system
        - run: pipenv install --system
          working-directory: nat-lab
        - run: pylint -f colorized . --ignore telio_bindings.py
          working-directory: nat-lab
  natlab-typecheck:
      needs: uniffi-bindings
      runs-on: ubuntu-22.04
      steps:
        - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
        - uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
          with:
            name: telio_bindings.py
            path: dist/bindings
        - run: cp dist/bindings/telio_bindings.py nat-lab/tests/uniffi/telio_bindings.py
        - run: pip3 install --no-deps -r requirements.txt
        - run: pipenv install --system
        - run: pipenv install --system
          working-directory: nat-lab
        - run: mypy .
          working-directory: nat-lab
  unused-features:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
      - uses: actions-rs/toolchain@b2417cde72dcf67f306c0ae8e0828a81bf0b189f # v1.0.6
        with:
          toolchain: 1.80.0
          override: true
          default: true
      - name: Install cargo-unusued-features
        run: cargo install --version 0.2.0 cargo-unused-features --locked
      - name: Run cargo-unused-features
        run: |
          for dir in ./crates/* ./clis/*; do
            pushd "$dir"
            unused-features analyze -l debug
            unused-features prune -l debug
            popd
          done
      - name: Check results
        run: |
          git restore Cargo.lock
          if ! git diff --quiet; then
            git diff
            exit 1
          fi
