# Building and Publishing Irene Cipher

This guide covers building, testing, and publishing the library to PyPI.

## Development Setup

### Prerequisites

- Rust 1.70+ (`rustup install stable`)
- Python 3.8+ 
- maturin (`pip install maturin`)

### Local Development Build

```bash
# Build and install in development mode
maturin develop

# Build with optimizations
maturin develop --release

# Run tests
cargo test

# Run Python examples
python examples.py
```

## Building Wheels

### Build for current platform

```bash
maturin build --release
```

Output: `target/wheels/*.whl`

### Build for all platforms (using GitHub Actions or manylinux)

```bash
# Linux (using manylinux)
docker run --rm -v $(pwd):/io konstin2/maturin build --release

# macOS (requires macOS machine)
maturin build --release --target universal2-apple-darwin

# Windows (requires Windows machine)
maturin build --release
```

## Testing

### Rust Tests

```bash
# Run all tests
cargo test

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_encrypt_decrypt_roundtrip
```

### Python Tests

```bash
# Install in development mode
maturin develop --release

# Run examples
python examples.py
```

## Publishing to PyPI

### Setup

1. Create account on [PyPI](https://pypi.org/)
2. Create API token
3. Configure credentials:

```bash
# Option 1: Environment variable
export MATURIN_PYPI_TOKEN=pypi-...

# Option 2: Use keyring
pip install keyring
keyring set https://upload.pypi.org/legacy/ __token__
```

### Publish

```bash
# Build wheels for all platforms first
# (usually done via CI/CD)

# Test on TestPyPI first
maturin publish --repository testpypi

# Publish to PyPI
maturin publish
```

### GitHub Actions CI/CD

Create `.github/workflows/release.yml`:

```yaml
name: Release

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
    
    steps:
    - uses: actions/checkout@v3
    
    - uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
    
    - uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
        override: true
    
    - name: Build wheels
      run: |
        pip install maturin
        maturin build --release
    
    - name: Upload wheels
      uses: actions/upload-artifact@v3
      with:
        name: wheels
        path: target/wheels/*.whl

  publish:
    needs: build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/download-artifact@v3
      with:
        name: wheels
    
    - name: Publish to PyPI
      env:
        MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
      run: |
        pip install maturin
        maturin upload --skip-existing wheels/*.whl
```

## Version Management

Update version in:
1. `Cargo.toml` - `version = "x.y.z"`
2. `pyproject.toml` - `version = "x.y.z"`

```bash
# Tag release
git tag v0.1.0
git push origin v0.1.0
```

## Documentation

### Generate Rust docs

```bash
cargo doc --open
```

### Update Python docs

Docstrings are in `src/python_bindings.rs` and automatically included in the module.

## Performance Profiling

```bash
# Install profiling tools
cargo install cargo-flamegraph

# Profile
cargo flamegraph --bin=your_binary

# Benchmark
cargo bench
```

## Security Audit

```bash
# Install audit tool
cargo install cargo-audit

# Run audit
cargo audit

# Update dependencies
cargo update
cargo audit
```

## Troubleshooting

### Build fails on Windows

- Ensure Visual Studio Build Tools are installed
- Use `x64 Native Tools Command Prompt`

### Import error in Python

```bash
# Rebuild and reinstall
maturin develop --release

# Check module is installed
python -c "import irene_cipher; print(dir(irene_cipher))"
```

### Tests fail

```bash
# Clean build
cargo clean
cargo test

# Verbose output
cargo test -- --nocapture
```
