name: cargo

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build and test
    strategy:
      matrix:
        os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Build
        run: cargo build --verbose
      - name: Run tests
        run: cargo test --verbose
      - name: Run doc tests
        run: cargo test --doc --verbose

  no_std:
    name: no_std compatibility
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: thumbv7em-none-eabihf
      - name: Build for no_std (embedded target)
        run: cargo build --target thumbv7em-none-eabihf --verbose
      - name: Build release for no_std
        run: cargo build --target thumbv7em-none-eabihf --release --verbose

  rustfmt_and_clippy:
    name: Check rustfmt style && run clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      - name: Run clippy
        run: cargo clippy --verbose -- -D warnings
      - name: Check formatting
        run: cargo fmt --all -- --check
