name: Build and Test

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

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        rust: [stable]

    steps:
    - uses: actions/checkout@v4
    
    - name: Install Rust
      uses: actions-rs/toolchain@v1
      with:
        toolchain: ${{ matrix.rust }}
        override: true
        components: rustfmt, clippy
    
    - name: Cache dependencies
      uses: actions/cache@v3
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: ${{ runner.os }}-cargo-
    
    - name: Format check
      uses: actions-rs/cargo@v1
      with:
        command: fmt
        args: --all -- --check
    
    - name: Clippy check
      uses: actions-rs/cargo@v1
      with:
        command: clippy
        args: -- -D warnings
    
    - name: Build
      uses: actions-rs/cargo@v1
      with:
        command: build
        args: --verbose
    
    - name: Run tests
      uses: actions-rs/cargo@v1
      with:
        command: test
        args: --verbose
