name: CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

env:
  CARGO_UNSTABLE_SPARSE_REGISTRY: true
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt
      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v2
      - uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

  check-stable:
    name: Cargo check (stable)
    runs-on: ubuntu-latest
    needs: fmt
    strategy:
      matrix:
        TARGET: [riscv64imac-unknown-none-elf, riscv32imac-unknown-none-elf]
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          target: ${{ matrix.TARGET }}
      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.TARGET }}
      - name: Check (no default features)
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --target ${{ matrix.TARGET }} --verbose
      - name: Check (machine)
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --target ${{ matrix.TARGET }} --features "machine" --verbose

  check-nightly:
    name: Cargo check (nightly)
    runs-on: ubuntu-latest
    needs: fmt
    strategy:
      matrix:
        TARGET: [riscv64imac-unknown-none-elf, riscv32imac-unknown-none-elf]
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: nightly
          override: true
          target: ${{ matrix.TARGET }}
      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.TARGET }}

  tests:
    name: Run tests
    needs: fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          components: clippy
          override: true
      - name: Cache Dependencies
        uses: Swatinem/rust-cache@v2
      - name: Clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
      - name: Run tests (no default features)
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --verbose
      - name: Run tests (machine)
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --features "machine" --verbose