Skip to content

About

This repository provides an example how rust proto gen might work with google protobuf

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Rust Bazel Protocol Buffers Generation Example

This repository demonstrates how to generate Rust code from Protocol Buffer definitions using Bazel with the rules_rust and protobuf integration.

Overview

This project showcases:

  • Setting up Bazel with Protocol Buffers and Rust
  • Generating Rust code from .proto files
  • Handling dependencies between proto files
  • Using Google's well-known types (like google.protobuf.Timestamp)
  • Applying patches to resolve compatibility issues

Project Structure

.
├── MODULE.bazel                    # Bazel module configuration
├── BUILD                          # Root build file
├── proto/
│   ├── BUILD.bazel                # Proto build rules for foo.proto
│   ├── foo.proto                  # Main proto definition
│   └── bar/
│       ├── BUILD.bazel            # Proto build rules for bar.proto
│       └── bar.proto              # Dependency proto definition
└── third_party/patches/
    ├── BUILD                      # Build file for patches
    └── protobuf_rules_rust.patch  # Compatibility patch for protobuf + rules_rust

Protocol Buffer Definitions

proto/foo.proto

  • Uses Protocol Buffers edition 2023
  • Defines a Foo message with various field types including:
    • Basic types (int32, string)
    • Repeated fields (repeated int32)
    • Message references (references Bar from bar.proto)
    • Google well-known types (google.protobuf.Timestamp)
  • Includes an enum ProductAttribute

proto/bar/bar.proto

  • Simple message definition that foo.proto depends on
  • Demonstrates cross-package proto dependencies

Bazel Configuration

Dependencies

  • protobuf: Version 32.1 with custom patches
  • rules_rust: Version 0.65.0

Key Features

  1. Patched Protobuf Integration: Uses a patch file to resolve compatibility issues between protobuf and rules_rust
  2. Rust Proto Libraries: Generates rust_proto_library targets from proto_library definitions
  3. Cross-package Dependencies: Shows how proto files in different directories can reference each other

Patch Details

The protobuf_rules_rust.patch addresses several compatibility issues:

  • Updates rules_rust version from 0.56.0 to 0.65.0
  • Configures Rust toolchain with edition 2021 and Rust version 1.79.0
  • Fixes package visibility and label resolution issues
  • Corrects kernel selection logic for UPB vs CPP backends

Build Targets

Proto Libraries

  • //proto:foo_proto - Protocol buffer library for foo.proto
  • //proto/bar:bar_proto - Protocol buffer library for bar.proto

Rust Libraries

  • //proto:foo_rust_proto - Generated Rust code for foo.proto
  • //proto/bar:bar_rust_proto - Generated Rust code for bar.proto

Building

To build all targets:

bazel build //...

To build specific Rust proto libraries:

bazel build //proto:foo_rust_proto
bazel build //proto/bar:bar_rust_proto

Dependencies and Imports

The project demonstrates proper dependency management:

  • foo.proto imports and uses bar.proto
  • Both proto files use Google's well-known types
  • Bazel build files properly declare dependencies using deps attribute

Compatibility Notes

This setup has been tested with:

  • Bazel (latest)
  • Protocol Buffers 32.1
  • rules_rust 0.65.0
  • Rust 1.79.0

The patch file ensures compatibility between these specific versions and may need updates for different version combinations.

Files Generated

When built, Bazel will generate Rust code in the bazel-bin directory structure, providing Rust structs and implementations for all defined Protocol Buffer messages and enums.

About

This repository provides an example how rust proto gen might work with google protobuf

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages