Contributing Guide¶
Thank you for your interest in contributing to protoruf! This guide covers setup, development workflow, and best practices.
Development Setup¶
Prerequisites¶
- Python 3.12+
- Rust (1.70+) — Install via rustup
- uv — Fast Python package manager
For the Node.js and browser (WASM) bindings, you also need:
- Node.js 20+ — Install via nvm or nodejs.org
- wasm-pack — builds the WASM package
- The
wasm32-unknown-unknownRust target:
1. Clone the Repository¶
2. Install Dependencies¶
Python:
This installs all development dependencies including maturin, pytest, and mypy.
Node.js / WASM (only if you work on those bindings):
This installs the JS dev dependencies (@napi-rs/cli, vitest, protobufjs).
3. Build the Bindings¶
Python extension:
This compiles the Rust code and installs the Python package in development mode.
Node.js addon (napi) and WASM package:
# Both at once (native addon + WASM)
npm run build:js
# Or individually
npm run build:debug # native Node addon (napi) -> dist/
npm run build:wasm # WASM package -> dist/wasm/
npm run test:js runs build:js automatically (via pretest:js), so you rarely
need to build by hand before testing.
Project Structure¶
protoruf/
├── python/protoruf/ # Python package
│ ├── __init__.py # Public API wrappers
│ ├── _protoruf.pyi # Type stubs for Rust extension
│ ├── compiler.py # Proto compilation utilities
│ └── py.typed # PEP 561 type marker
├── src/ # Rust source code
│ ├── lib.rs # Crate root + feature-gated binding modules
│ ├── core.rs # Core logic (pure Rust, no FFI)
│ ├── python.rs # Python bindings (PyO3, feature "python")
│ ├── node.rs # Node.js bindings (napi, feature "node")
│ └── wasm.rs # Browser/WASM bindings (wasm-bindgen, feature "wasm")
├── tests/ # Test suites
│ ├── proto/ # Test proto files
│ ├── test_models.py # Pydantic test models
│ ├── test_*.py # Python tests
│ └── js/ # JS tests (node + wasm + parity, vitest)
├── examples/ # Usage examples (Python + examples/js/)
├── package.json # Node build/test scripts (napi, wasm-pack, vitest)
└── docs/ # Documentation
Each binding is a thin wrapper over src/core.rs and is gated behind its own
Cargo feature (python, node, wasm), so building one never pulls in the
others' dependencies.
Development Workflow¶
Make Changes¶
- Edit Rust code in
src/(shared logic incore.rs, bindings inpython.rs/node.rs/wasm.rs) - Edit Python code in
python/protoruf/, or JS/TS inexamples/js/andtests/js/ - Rebuild the affected binding(s):
Run Tests¶
Python Tests¶
Rust Tests¶
Unit tests live in src/*.rs under #[cfg(test)]. Run them across all targets
(the same as CI):
Node / WASM Tests (JavaScript)¶
The vitest suite (tests/js/) covers the native Node addon, the WASM build, and
node↔wasm parity. pretest:js builds both bindings first, so a single command
does everything:
All Tests¶
Rust Validation (format & lint)¶
These are the exact commands CI runs (.github/workflows/rust-test.yml) — run
them before pushing:
# Format the code in place
cargo fmt --all
# Validation (what CI checks): formatting is correct, no clippy warnings, tests pass
cargo fmt --all -- --check
cargo clippy --all-targets -- -D warnings
cargo test --all-targets
cargo clippy treats every warning as an error (-D warnings), so the build
fails on any lint. Run cargo fmt --all first to auto-fix formatting.
Type Checking¶
Pre-commit Checklist¶
Before committing, ensure all checks pass:
# 1. Python tests
uv run pytest tests/ -v
# 2. Rust formatting (CI checks this)
cargo fmt --all -- --check
# 3. Rust lints (warnings fail the build)
cargo clippy --all-targets -- -D warnings
# 4. Rust tests
cargo test --all-targets
# 5. Type checking
uv run mypy python/protoruf/ --ignore-missing-imports
# 6. Build verification
uv run maturin develop
# 7. Node / WASM tests (if you touched core.rs or the node/wasm bindings)
npm run test:js
Adding Features¶
Rust Changes¶
- Add logic to
src/core.rs(pure Rust, the single source of truth) - Expose it in each binding you support:
src/python.rs(PyO3),src/node.rs(napi),src/wasm.rs(wasm-bindgen) — keep the signatures consistent - Update type stubs in
python/protoruf/_protoruf.pyi - Write tests in Rust (
#[cfg(test)]incore.rs), Python (tests/), and JS (tests/js/)
Python Changes¶
- Update wrappers in
python/protoruf/__init__.py - Update type stubs if signatures change
- Add tests to
tests/
Documentation¶
Update relevant docs in docs/:
- API changes → docs/api/reference.md
- New features → docs/guide/
- Usage examples → docs/examples/
Testing Guidelines¶
Test Proto Files¶
Test proto files are in tests/proto/. When adding new proto features:
- Update or create a
.protofile - Add corresponding Python test
- Test both conversion directions (JSON ↔ Protobuf)
Example Test¶
def test_nested_message():
descriptor = compile_proto("tests/proto/message.proto")
json_data = '''
{
"id": "123",
"nested": {
"field1": "value1",
"field2": 42
}
}
'''
protobuf_bytes = json_to_protobuf(json_data, descriptor, "message.Message")
result = protobuf_to_json(protobuf_bytes, descriptor, "message.Message")
# Verify round-trip
assert json.loads(json_data) == json.loads(result)
Code Style¶
Rust¶
- Follow standard Rust conventions — format with
cargo fmt --all - Keep
cargo clippy --all-targets -- -D warningsclean (no warnings) - Use descriptive variable names
- Add comments for complex logic
- Write unit tests for core functions
Python¶
- Follow PEP 8
- Use type hints everywhere
- Keep functions focused and small
- Docstrings for all public functions
Building for Distribution¶
Build Wheels (Python)¶
This creates distributable wheels in target/wheels/.
Build npm Packages (Node / WASM)¶
npm run build # release native addon (napi)
npm run pack:wasm # release WASM package for the browser
Local Install¶
Debugging¶
Enable Logging¶
Add print statements in Python:
Rust Debugging¶
Use eprintln! for debugging Rust code:
Test Individual Functions¶
# Test specific Rust module
cargo test core
# Test specific Python module
uv run pytest tests/test_rust_json_protobuf.py -v
Submitting Changes¶
- Fork the repository
- Create a branch for your feature:
git checkout -b feature/my-feature - Make changes and ensure all tests pass
- Update documentation if applicable
- Commit with clear messages:
git commit -m "feat: add support for maps" - Push to your fork:
git push origin feature/my-feature - Open a Pull Request with a clear description of changes
Commit Message Convention¶
Use conventional commits:
feat:— New featurefix:— Bug fixdocs:— Documentation changestest:— Test additions or changesrefactor:— Code refactoringchore:— Maintenance tasks
Reporting Issues¶
When reporting bugs:
- Include Python version
- Include protoruf version
- Provide a minimal reproducible example
- Include error messages and stack traces
- Describe expected vs actual behavior
Performance Considerations¶
When making changes:
- Avoid unnecessary allocations in Rust
- Use
&strinstead ofStringwhen possible - Profile before optimizing
- Benchmark significant changes
License¶
By contributing, you agree that your contributions will be licensed under the MIT License.