name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: rust
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: rust
      - run: cargo fmt --check
      - run: cargo clippy --all-targets -- -D warnings
      - run: cargo test

  skill-lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install PyYAML
        run: python3 -m pip install --quiet pyyaml
      - name: Validate SKILL.md frontmatter
        run: |
          python3 - <<'PY'
          import sys, pathlib, yaml
          errs = 0
          checked = 0
          for p in sorted(pathlib.Path('.').rglob('SKILL.md')):
              checked += 1
              text = p.read_text(encoding='utf-8')
              if not text.startswith('---'):
                  print(f"{p}: missing leading '---' frontmatter delimiter")
                  errs += 1
                  continue
              parts = text.split('---', 2)
              if len(parts) < 3:
                  print(f"{p}: malformed frontmatter (no closing '---')")
                  errs += 1
                  continue
              try:
                  yaml.safe_load(parts[1])
              except yaml.YAMLError as e:
                  print(f"{p}: invalid YAML: {e}")
                  errs += 1
          if checked == 0:
              print("No SKILL.md files found")
              sys.exit(1)
          if errs:
              sys.exit(1)
          print(f"All {checked} SKILL.md file(s) parse cleanly")
          PY
