name: CI

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

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          path: meta_plugin_protocol

      - name: Create workspace Cargo.toml
        shell: bash
        run: |
          cat > Cargo.toml << 'EOF'
          [workspace]
          members = ["meta_plugin_protocol"]
          resolver = "2"

          [workspace.package]
          version = "0.1.0"
          edition = "2021"
          license = "MIT"
          repository = "https://github.com/gitkb/meta"
          EOF

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache Rust
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: meta_plugin_protocol

      - name: Run tests
        working-directory: meta_plugin_protocol
        run: cargo test --all-features

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          path: meta_plugin_protocol

      - name: Create workspace Cargo.toml
        run: |
          cat > Cargo.toml << 'EOF'
          [workspace]
          members = ["meta_plugin_protocol"]
          resolver = "2"

          [workspace.package]
          version = "0.1.0"
          edition = "2021"
          license = "MIT"
          repository = "https://github.com/gitkb/meta"
          EOF

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache Rust
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: meta_plugin_protocol

      - name: Run clippy
        working-directory: meta_plugin_protocol
        run: cargo clippy --all-targets --all-features -- -D warnings

  format:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          path: meta_plugin_protocol

      - name: Create workspace Cargo.toml
        run: |
          cat > Cargo.toml << 'EOF'
          [workspace]
          members = ["meta_plugin_protocol"]
          resolver = "2"

          [workspace.package]
          version = "0.1.0"
          edition = "2021"
          license = "MIT"
          repository = "https://github.com/gitkb/meta"
          EOF

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      - name: Check formatting
        working-directory: meta_plugin_protocol
        run: cargo fmt --all -- --check
