name: build

on:
  pull_request:
    paths-ignore:
      - "**/*.md"
  push:
    branches:
      - "master"
    paths-ignore:
      - "**/*.md"
  merge_group:
    types:
      - checks_requested

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: -D warnings
  RUSTDOCFLAGS: -D warnings

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          targets: x86_64-unknown-linux-musl
          components: rustfmt
      - name: Run Cargo Fmt
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable
          targets: x86_64-unknown-linux-musl
          components: clippy
      - name: Get build cache
        uses: Swatinem/rust-cache@v2
      - name: Run clippy
        run: cargo clippy --release --all-targets -- -D warnings

  test:
    name: Test
    runs-on: ${{ matrix.job.os }}
    strategy:
      matrix:
        job:
          - os: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-unknown-linux-musl
          toolchain: stable
      - name: Get build cache
        uses: Swatinem/rust-cache@v2
      - name: Run Cargo Test
        run: cargo test -r --all-targets --workspace

  result:
    name: Result (CI)
    runs-on: ubuntu-latest
    needs:
      - fmt
      - clippy
      - test
    steps:
      - name: Mark the job as successful
        run: exit 0
        if: "success()"
      - name: Mark the job as unsuccessful
        run: exit 1
        if: "!success()"
