Loading Rust to Protobuf Converter...
How to Turn Rust Structs into a .proto File
Moving a Rust service to gRPC, or sharing its data with services in other languages, usually starts with writing message definitions that mirror structs you already have. Paste those structs here and you get a proto3 schema with the field types, numbers and JSON names worked out, ready to review and adjust.
Paste your Rust types
Paste the struct and enum definitions you want as messages, with their #[derive] and #[serde] attributes, or upload a .rs file. A whole file is fine: use lines, impl blocks and functions are skipped, and types inside mod blocks are read too. Click Sample to try a small example first.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Order {
pub order_id: u64,
pub customer: String,
pub total: f64,
pub note: Option<String>,
pub items: Vec<Item>,
pub status: Status,
}
#[derive(Serialize, Deserialize)]
pub struct Item {
pub sku: String,
#[serde(rename = "qty")]
pub quantity: u32,
}
#[derive(Serialize, Deserialize)]
pub enum Status {
Pending,
Shipped,
}Check the generated .proto
A moment after you stop typing, the proto3 schema appears on the right. Comments at the top and above single fields explain every choice that needed a decision, such as a u128 kept as a string or a list of lists wrapped in a helper message. A syntax mistake in the Rust code gives you the line and column instead.
The example above comes out like this. order_id is a u64, so it becomes uint64; rename_all = "camelCase" already matches protobuf's own JSON names, while the qty rename on Item needs a json_name:
syntax = "proto3";
// Notes:
// - Enum values get the enum name as a prefix (for example STATUS_ACTIVE) because protobuf enum values share one scope, and a zero value (..._UNSPECIFIED) comes first unless the Rust code sets one to 0. prost strips the prefix again. In JSON, serde writes the variant name ("Active"), protobuf the value name.
// - No package line is written. Add one (for example: package myapp.v1;) before you build code from it.
// - Field numbers start at 1 in the order the fields appear. Once the schema is in use, keep each number for good.
message Order {
uint64 order_id = 1;
string customer = 2;
double total = 3;
optional string note = 4;
repeated Item items = 5;
Status status = 6;
}
message Item {
string sku = 1;
uint32 quantity = 2 [json_name = "qty"];
}
enum Status {
STATUS_UNSPECIFIED = 0;
STATUS_PENDING = 1;
STATUS_SHIPPED = 2;
}Copy or download it
Copy puts the schema on your clipboard and Download saves it as schema.proto. Add a package line, then compile it with protoc, prost-build or tonic-build like any other .proto file.
What is Rust to Protobuf Conversion?
Protocol Buffers describe data as messages with numbered, typed fields, and code generators such as prost turn a .proto file back into Rust structs. Going the other way is mostly a translation of names and types, plus a few decisions where protobuf has no direct form, such as nested lists, 128-bit integers and enums that carry data.
JSON names follow serde's attributes, so a service that already speaks JSON keeps its keys. To start from JSON data instead, try JSON to Protobuf; to go the other way, Protobuf to Rust writes prost structs, and the Proto File Validator checks a schema after you edit it.
Frequently Asked Questions
What Rust code does the converter read?
Type definitions: structs with named fields, tuple structs, unit structs, enums and type aliases, including generics, where clauses, pub(crate) and nested mod blocks. Everything else (use lines, impl blocks, functions, constants, macros) is skipped. The code is read, never compiled or run. A struct literal such as User { id: 1 } or a vec![...] of them describes data, not types, so the page asks for the definitions instead.
How are Rust types mapped to protobuf types?
i8, i16 and i32 become int32; u8, u16 and u32 become uint32; i64 and isize become int64; u64 and usize become uint64. f32 is float, f64 is double, bool is bool, and String, &str and char are string. Vec<u8> and [u8; N] become bytes. Option<T> becomes an optional field, Vec, VecDeque, HashSet and BTreeSet become repeated fields, and HashMap or BTreeMap become a map. Structs become messages, enums with plain variants become proto enums, and Box, Rc, Arc and Cow are looked through. A long ID such as 8901240544102066246 fits uint64 or int64 exactly; i128 and u128 have no protobuf type, so they become string with a comment.
What happens to serde attributes?
Proto field names follow the Rust field names, which is what prost generates back. When serde writes a different JSON key, through rename or rename_all, that key is set as json_name, so protobuf JSON uses the same key as serde. skip and skip_serializing leave the field out, flatten copies the inner struct's fields into the message, and newtypes, transparent structs and type aliases are written as the type they wrap.
How are enums converted?
An enum whose variants carry no data becomes a proto enum. Each value gets the enum name as a prefix (STATUS_ACTIVE), because protobuf enum values share one scope, and a zero value named ..._UNSPECIFIED comes first, as proto3 requires. If the Rust code gives the variants numbers, those numbers are kept. An enum with data becomes a message with a oneof: one field per variant, a struct variant gets its own message, and a unit variant uses google.protobuf.Empty.
What can protobuf not express directly?
Protobuf has no list of lists, no list of Options and no optional map, so Vec<Vec<i32>> becomes a repeated helper message (Int32List), None items in a list are not kept, and an Option<Vec<T>> reads back as an empty list. Tuples become small messages with item_0, item_1 fields. Generic parameters and types that are not in the pasted code become string, with a comment telling you to paste the missing definition. Duration and SystemTime map to google.protobuf.Duration and Timestamp, and serde_json::Value to google.protobuf.Value, with the import lines added for you.
Can I use the schema with prost, tonic or gRPC?
Yes, after one small step: add a package line, since the output leaves that choice to you. Then compile it like any .proto file, with protoc, prost-build or tonic-build, and add your service definitions for gRPC. The page checks the Rust code's syntax, not its types, so it can't tell whether a type you reference really exists in your crate; such types are listed in comments.
Is my code uploaded anywhere?
No. The conversion runs in your browser, the code is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploaded .rs files are read in the browser too, up to 10 MB.
Related Tools
Protobuf to Table
Convert Protocol Buffer text format data to HTML table format
Protobuf Fixer
Fix broken .proto schemas automatically, repair syntax errors, and validate Protobuf structure
Protobuf to JSON
Convert Protocol Buffer messages to JSON format
JSON to Protobuf
Convert JSON data to Protocol Buffer format
Protobuf to TypeScript
Generate TypeScript interfaces and types from Protocol Buffer schemas
TypeScript to Protobuf
Generate Protocol Buffer schemas from TypeScript interfaces