Quick Start (Rust)¶
Use the protoruf engine directly from a Rust project.
Step 1: Add the dependency¶
The crate is the engine behind every binding. Depend on it by git (or a local path):
All conversion functions live in the core module and return Result<_, String>.
Step 2: Compile a schema¶
use protoruf::core;
// From disk:
let descriptor = core::compile_proto("message.proto", None)?;
// ...or fully in memory (no file, works anywhere including WASM):
use std::collections::HashMap;
let files = HashMap::from([(
"message.proto".to_string(),
r#"syntax = "proto3"; package message; message Message { string id = 1; string content = 2; }"#.to_string(),
)]);
let descriptor = core::compile_proto_from_sources(files, "message.proto")?;
Step 3: JSON → Protobuf → JSON¶
let json = r#"{"id":"123","content":"Hello"}"#;
// JSON -> Protobuf bytes
let wire = core::json_to_protobuf_bytes(json, &descriptor, "message.Message")?;
// Protobuf bytes -> JSON (pretty)
let out = core::protobuf_to_json_string(&wire, &descriptor, "message.Message", true)?;
println!("{out}");
Complete example¶
use protoruf::core;
fn main() -> Result<(), String> {
let descriptor = core::compile_proto("message.proto", None)?;
let json = r#"{"id":"123","content":"Hello"}"#;
let wire = core::json_to_protobuf_bytes(json, &descriptor, "message.Message")?;
let out = core::protobuf_to_json_string(&wire, &descriptor, "message.Message", true)?;
println!("{out}");
Ok(())
}
What's Next?¶
- Basic Usage — every supported Protobuf feature
- Proto Compilation — imports, well-known types, in-memory compilation
- Performance & Patterns — the cached path for hot loops
- API Reference