name: Rust

on:
  pull_request:
  push:
    branches:
      - master

env:
  CARGO_TERM_COLOR: always

jobs:
  style:
    name: Check Style
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Sources
        uses: actions/checkout@v6

      - name: Install Rust
        run: |
          rustup update stable
          rustup component add --toolchain stable rustfmt
          rustup default stable

      - name: Run Cargo Format
        run: cargo fmt --all -- --check

  lint:
    name: Lint
    needs: [ style ]
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Sources
        uses: actions/checkout@v6

      - name: Install Rust
        run: |
          rustup update stable
          rustup component add --toolchain stable clippy
          rustup default stable

      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry/
            ~/.cargo/git/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Run Cargo Clippy
        run: cargo clippy -q -- --no-deps

  test:
    name: Test
    needs: [ style, lint ]
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Sources
        uses: actions/checkout@v6

      - name: Install Rust
        run: |
          rustup update stable
          rustup default stable

      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry/
            ~/.cargo/git/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Compile (Tests)
        run: cargo test -q --no-run

      - name: Run test
        run: cargo test -q -- --nocapture

  build:
    name: Build
    needs: [ style, lint, test ]
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Sources
        uses: actions/checkout@v6

      - name: Install Rust
        run: |
          rustup update stable
          rustup default stable

      - name: Cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry/
            ~/.cargo/git/
            target/
          key: ${{ runner.os }}-cargo-release-${{ hashFiles('**/Cargo.lock') }}

      - name: Run Cargo Build
        run: cargo build -q --release

  doc:
    name: Build Docs
    needs: [ style, lint, test ]
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Sources
        uses: actions/checkout@v6

      - name: Install Rust
        run: |
          rustup update stable
          rustup default stable

      - name: Build Documentation
        run: cargo rustdoc -q
