name: Documentation

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

concurrency:
  group: docs-deploy
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  docs:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v5
      with:
        submodules: recursive
    
    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
    
    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-docs-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-docs-
    
    - name: Generate documentation
      run: cargo doc --no-deps --document-private-items
    
    - name: Deploy to GitHub Pages
      if: github.ref == 'refs/heads/main' && github.event_name == 'push'
      uses: peaceiris/actions-gh-pages@v4
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        publish_dir: ./target/doc
        destination_dir: docs