Refactor crate into multiple subcrates

This commit is contained in:
2026-05-04 22:45:55 -07:00
parent d71d96c15f
commit ea68537f0b
48 changed files with 9839 additions and 366 deletions
+10
View File
@@ -0,0 +1,10 @@
[package]
name = "roto-codegen"
version = "0.1.0"
edition = "2024"
[dependencies]
roto-runtime = { path = "../runtime" }
clap = { version = "4", features = ["derive"] }
log = "0.4"
env_logger = "0.11"
+1
View File
@@ -0,0 +1 @@
bench/
Binary file not shown.
Binary file not shown.
Binary file not shown.
+39
View File
@@ -0,0 +1,39 @@
d_val: 3.1415926535
f_val: 2.71828
i32_val: 42
i64_val: 123456789012345
u32_val: 1000
u64_val: 18446744073709551615
si32_val: -42
si64_val: -123456789012345
fx32_val: 123456
fx64_val: 1234567890123456789
sfx32_val: -123456
sfx64_val: -1234567890123456789
b_val: true
s_val: "Hello Roto!"
bytes_val: "SGVsbG8gUm90byE="
status: ACTIVE
repeated_i32: 1
repeated_i32: 2
repeated_i32: 3
repeated_i32: 4
repeated_i32: 5
repeated_string: "one"
repeated_string: "two"
repeated_string: "three"
repeated_nested {
id: 101
name: "Nested 1"
active: true
}
repeated_nested {
id: 102
name: "Nested 2"
active: false
}
single_nested {
id: 200
name: "Single Nested"
active: true
}
Binary file not shown.
+53
View File
@@ -0,0 +1,53 @@
syntax = "proto3";
package roto.test;
// A comprehensive message containing all primitive types and complex structures
// to test the proto-to-rust codegen and runtime accessors.
message ComplexMessage {
// --- Floating Point ---
double d_val = 1;
float f_val = 2;
// --- Integers (Variable Length) ---
int32 i32_val = 3;
int64 i64_val = 4;
uint32 u32_val = 5;
uint64 u64_val = 6;
sint32 si32_val = 7;
sint64 si64_val = 8;
// --- Integers (Fixed Length) ---
fixed32 fx32_val = 9;
fixed64 fx64_val = 10;
sfixed32 sfx32_val = 11;
sfixed64 sfx64_val = 12;
// --- Other Primitives ---
bool b_val = 13;
string s_val = 14;
bytes bytes_val = 15;
// --- Enumerations ---
enum Status {
UNKNOWN = 0;
ACTIVE = 1;
INACTIVE = 2;
DELETED = 3;
}
Status status = 16;
// --- Repeated Fields ---
// Testing packed primitives and non-packed types
repeated int32 repeated_i32 = 17;
repeated string repeated_string = 18;
repeated NestedMessage repeated_nested = 19;
// --- Nested Messages ---
message NestedMessage {
int32 id = 1;
string name = 2;
bool active = 3;
}
NestedMessage single_nested = 20;
}
@@ -0,0 +1,180 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// Author: kenton@google.com (Kenton Varda)
//
// protoc (aka the Protocol Compiler) can be extended via plugins. A plugin is
// just a program that reads a CodeGeneratorRequest from stdin and writes a
// CodeGeneratorResponse to stdout.
//
// Plugins written using C++ can use google/protobuf/compiler/plugin.h instead
// of dealing with the raw protocol defined here.
//
// A plugin executable needs only to be placed somewhere in the path. The
// plugin should be named "protoc-gen-$NAME", and will then be used when the
// flag "--${NAME}_out" is passed to protoc.
syntax = "proto2";
package google.protobuf.compiler;
option java_package = "com.google.protobuf.compiler";
option java_outer_classname = "PluginProtos";
import "google/protobuf/descriptor.proto";
option csharp_namespace = "Google.Protobuf.Compiler";
option go_package = "google.golang.org/protobuf/types/pluginpb";
// The version number of protocol compiler.
message Version {
optional int32 major = 1;
optional int32 minor = 2;
optional int32 patch = 3;
// A suffix for alpha, beta or rc release, e.g., "alpha-1", "rc2". It should
// be empty for mainline stable releases.
optional string suffix = 4;
}
// An encoded CodeGeneratorRequest is written to the plugin's stdin.
message CodeGeneratorRequest {
// The .proto files that were explicitly listed on the command-line. The
// code generator should generate code only for these files. Each file's
// descriptor will be included in proto_file, below.
repeated string file_to_generate = 1;
// The generator parameter passed on the command-line.
optional string parameter = 2;
// FileDescriptorProtos for all files in files_to_generate and everything
// they import. The files will appear in topological order, so each file
// appears before any file that imports it.
//
// Note: the files listed in files_to_generate will include runtime-retention
// options only, but all other files will include source-retention options.
// The source_file_descriptors field below is available in case you need
// source-retention options for files_to_generate.
//
// protoc guarantees that all proto_files will be written after
// the fields above, even though this is not technically guaranteed by the
// protobuf wire format. This theoretically could allow a plugin to stream
// in the FileDescriptorProtos and handle them one by one rather than read
// the entire set into memory at once. However, as of this writing, this
// is not similarly optimized on protoc's end -- it will store all fields in
// memory at once before sending them to the plugin.
//
// Type names of fields and extensions in the FileDescriptorProto are always
// fully qualified.
repeated FileDescriptorProto proto_file = 15;
// File descriptors with all options, including source-retention options.
// These descriptors are only provided for the files listed in
// files_to_generate.
repeated FileDescriptorProto source_file_descriptors = 17;
// The version number of protocol compiler.
optional Version compiler_version = 3;
}
// The plugin writes an encoded CodeGeneratorResponse to stdout.
message CodeGeneratorResponse {
// Error message. If non-empty, code generation failed. The plugin process
// should exit with status code zero even if it reports an error in this way.
//
// This should be used to indicate errors in .proto files which prevent the
// code generator from generating correct code. Errors which indicate a
// problem in protoc itself -- such as the input CodeGeneratorRequest being
// unparseable -- should be reported by writing a message to stderr and
// exiting with a non-zero status code.
optional string error = 1;
// A bitmask of supported features that the code generator supports.
// This is a bitwise "or" of values from the Feature enum.
optional uint64 supported_features = 2;
// Sync with code_generator.h.
enum Feature {
FEATURE_NONE = 0;
FEATURE_PROTO3_OPTIONAL = 1;
FEATURE_SUPPORTS_EDITIONS = 2;
}
// The minimum edition this plugin supports. This will be treated as an
// Edition enum, but we want to allow unknown values. It should be specified
// according the edition enum value, *not* the edition number. Only takes
// effect for plugins that have FEATURE_SUPPORTS_EDITIONS set.
optional int32 minimum_edition = 3;
// The maximum edition this plugin supports. This will be treated as an
// Edition enum, but we want to allow unknown values. It should be specified
// according the edition enum value, *not* the edition number. Only takes
// effect for plugins that have FEATURE_SUPPORTS_EDITIONS set.
optional int32 maximum_edition = 4;
// Represents a single generated file.
message File {
// The file name, relative to the output directory. The name must not
// contain "." or ".." components and must be relative, not be absolute (so,
// the file cannot lie outside the output directory). "/" must be used as
// the path separator, not "\".
//
// If the name is omitted, the content will be appended to the previous
// file. This allows the generator to break large files into small chunks,
// and allows the generated text to be streamed back to protoc so that large
// files need not reside completely in memory at one time. Note that as of
// this writing protoc does not optimize for this -- it will read the entire
// CodeGeneratorResponse before writing files to disk.
optional string name = 1;
// If non-empty, indicates that the named file should already exist, and the
// content here is to be inserted into that file at a defined insertion
// point. This feature allows a code generator to extend the output
// produced by another code generator. The original generator may provide
// insertion points by placing special annotations in the file that look
// like:
// @@protoc_insertion_point(NAME)
// The annotation can have arbitrary text before and after it on the line,
// which allows it to be placed in a comment. NAME should be replaced with
// an identifier naming the point -- this is what other generators will use
// as the insertion_point. Code inserted at this point will be placed
// immediately above the line containing the insertion point (thus multiple
// insertions to the same point will come out in the order they were added).
// The double-@ is intended to make it unlikely that the generated code
// could contain things that look like insertion points by accident.
//
// For example, the C++ code generator places the following line in the
// .pb.h files that it generates:
// // @@protoc_insertion_point(namespace_scope)
// This line appears within the scope of the file's package namespace, but
// outside of any particular class. Another plugin can then specify the
// insertion_point "namespace_scope" to generate additional classes or
// other declarations that should be placed in this scope.
//
// Note that if the line containing the insertion point begins with
// whitespace, the same whitespace will be added to every line of the
// inserted text. This is useful for languages like Python, where
// indentation matters. In these languages, the insertion point comment
// should be indented the same amount as any inserted code will need to be
// in order to work correctly in that context.
//
// The code generator that generates the initial file and the one which
// inserts into it must both run as part of a single invocation of protoc.
// Code generators are executed in the order in which they appear on the
// command line.
//
// If |insertion_point| is present, |name| must also be present.
optional string insertion_point = 2;
// The file contents.
optional string content = 15;
// Information describing the file content being inserted. If an insertion
// point is used, this information will be appropriately offset and inserted
// into the code generation metadata for the generated files.
optional GeneratedCodeInfo generated_code_info = 16;
}
repeated File file = 15;
}
File diff suppressed because it is too large Load Diff
+56
View File
@@ -0,0 +1,56 @@
syntax = "proto3";
message Tool {
string name = 1;
string version = 2;
bytes payload = 3;
bool is_active = 4;
int32 exploit_count = 5;
}
message Connection {
string host = 1;
int32 port = 2;
bool encrypted = 3;
int64 bandwidth_bps = 4;
bytes session_key = 5;
}
message Hacker {
string handle = 1;
string real_name = 2;
int32 age = 3;
float skill_level = 4; // Fixed32
bool is_elite = 5;
int64 crew_id = 6;
repeated string exploits = 7;
repeated Tool tools = 8;
Connection active_connection = 9;
}
message Worm {
string name = 1;
int32 variant = 2;
int64 size_bytes = 3;
bytes payload = 4;
bool polymorphic = 5;
repeated string targets = 6;
}
message Operation {
string codename = 1;
string target_corp = 2;
int64 timestamp = 3;
bool successful = 4;
bytes stolen_data = 5;
repeated Hacker crew = 6;
Worm worm = 7;
repeated string log_entries = 8;
int32 severity = 9;
}
message Campaign {
string name = 1;
repeated Operation operations = 2;
int64 total_bytes_stolen = 3;
}
+42
View File
@@ -0,0 +1,42 @@
use clap::Parser;
use roto_codegen::generator::generate_rust_code;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
use std::fs;
use std::path::PathBuf;
#[derive(Parser)]
#[command(
author,
version,
about = "Generates Rust accessor and builder code from a protobuf descriptor set"
)]
struct Args {
/// Path to the descriptor set file (.desc)
#[arg(short, long)]
input: PathBuf,
/// Path to the output directory
#[arg(short, long)]
output: PathBuf,
/// Files to generate. If omitted, all files are generated.
#[arg(short, long, value_delimiter = ',')]
files: Option<Vec<String>>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let data = fs::read(&args.input)?;
let set = FileDescriptorSet::new(&data).expect("Failed to parse FileDescriptorSet");
let files = generate_rust_code(&set, args.files.as_deref(), true);
for (filename, content) in files {
let path = args.output.join(filename);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, content)?;
}
Ok(())
}
+142
View File
@@ -0,0 +1,142 @@
use env_logger::init;
use log::{error, info};
use roto_codegen::generator::generate_rust_code;
use roto_codegen::google::protobuf::compiler::plugin::{
CodeGeneratorRequest, CodeGeneratorResponseBuilder, code_generator_response::FileBuilder,
};
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
// use roto_runtime::ProtoBuilder;
use std::io::{self, Read, Write};
fn main() {
// Initialize logger to print to stderr
init();
if let Err(e) = run() {
error!("Plugin error: {}", e);
std::process::exit(1);
}
}
fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
// 1. Read CodeGeneratorRequest from stdin
let mut stdin_buf = Vec::new();
io::stdin().read_to_end(&mut stdin_buf)?;
if stdin_buf.is_empty() {
error!("Received empty CodeGeneratorRequest from stdin");
return Err("Empty stdin".into());
}
let request = CodeGeneratorRequest::new(&stdin_buf)?;
// 2. Process request and get response bytes
let response_bytes = handle_request(&request)?;
// 3. Write response to stdout
io::stdout().write_all(&response_bytes)?;
info!("Successfully wrote CodeGeneratorResponse to stdout");
Ok(())
}
/// Core logic that transforms a CodeGeneratorRequest into a serialized CodeGeneratorResponse.
fn handle_request(
request: &CodeGeneratorRequest,
) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error>> {
// 2. Construct a FileDescriptorSet from the request's proto_files
let mut set_buf = Vec::new();
for file_res in request.proto_file() {
let (file_data, _) = file_res.map_err(|e| {
error!("Failed to iterate proto_file: {:?}", e);
e
})?;
// Tag 1, Length-delimited: (1 << 3) | 2 = 10
set_buf.push(10);
// Write length as varint
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size = roto_runtime::write_varint(len, &mut len_buf).map_err(|e| {
error!("Failed to write varint length: {:?}", e);
e
})?;
set_buf.extend_from_slice(&len_buf[..len_size]);
// Write data
set_buf.extend_from_slice(file_data);
}
let set = FileDescriptorSet::new(&set_buf)?;
let files_to_generate: Vec<String> = request
.file_to_generate()
.filter_map(|res| {
let (bytes, _) = res.ok()?;
std::str::from_utf8(bytes).ok().map(|s| s.to_string())
})
.collect();
// Generate the Rust code
info!("Generating Rust code from descriptor set...");
let generated_files = generate_rust_code(&set, Some(&files_to_generate), false);
// Construct the response
let mut response_buf = vec![0u8; 1024 * 1024 * 2]; // Allocate 2MB for response
let mut resp_builder = CodeGeneratorResponseBuilder::builder(&mut response_buf);
for (filename, content) in generated_files {
let mut file_buf = vec![0u8; 1024 * 1024 * 2];
let final_file = FileBuilder::builder(&mut file_buf)
.name(&filename)?
.content(&content)?
.finish()
.map_err(|e| {
error!("Failed to build ResponseFile {}: {:?}", filename, e);
e
})?;
resp_builder = resp_builder.file(final_file)?;
}
let final_response_slice = resp_builder.finish().map_err(|e| {
error!("Failed to finish CodeGeneratorResponse: {:?}", e);
e
})?;
// The finish() method returns a reference to the buffer.
// We convert it to a Vec<u8> to return it from this function.
Ok(final_response_slice.to_vec())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_handle_request_with_bin() {
// Note: this test assumes request.bin exists in the current working directory
// which is usually the project root during 'cargo test'.
let request_path = "request.bin";
if !std::path::Path::new(request_path).exists() {
return; // Skip if file is not available in the environment
}
let data = fs::read(request_path).expect("Failed to read request.bin");
let request =
CodeGeneratorRequest::new(&data).expect("Failed to parse CodeGeneratorRequest");
let result = handle_request(&request);
assert!(
result.is_ok(),
"handle_request should succeed with request.bin"
);
let response = result.unwrap();
assert!(
!response.is_empty(),
"The generated response should not be empty"
);
}
}
+490
View File
@@ -0,0 +1,490 @@
use crate::google::protobuf::descriptor::{
DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto,
FileDescriptorSet,
};
use roto_runtime::ProtoAccessor;
use std::collections::{HashMap, HashSet};
use std::str;
pub fn to_pascal_case(s: &str) -> String {
s.split('_')
.map(|word| {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
}
})
.collect()
}
pub fn to_snake_case(s: &str) -> String {
let mut result = String::new();
for (i, c) in s.chars().enumerate() {
if c.is_uppercase() {
if i > 0 {
result.push('_');
}
result.push(c.to_ascii_lowercase());
} else {
result.push(c);
}
}
result
}
fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) {
if label == 3 {
// LABEL_REPEATED
return (
"roto_runtime::RepeatedFieldIterator<'a>".to_string(),
"".to_string(), // Not used for repeated fields in the same way
);
}
match field_type {
9 => (
"&'a str".to_string(),
"str::from_utf8(bytes).map_err(|_| crate::RotoError::WireFormatViolation)".to_string(),
), // TYPE_STRING
1 => (
"f64".to_string(),
"Ok(f64::from_le_bytes(bytes.try_into().map_err(|_| crate::RotoError::WireFormatViolation)?))".to_string(),
), // TYPE_DOUBLE
2 => (
"f32".to_string(),
"Ok(f32::from_le_bytes(bytes.try_into().map_err(|_| crate::RotoError::WireFormatViolation)?))".to_string(),
), // TYPE_FLOAT
3 | 5 | 15 | 17 => (
"i32".to_string(),
"roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)".to_string(),
), // INT/SINT/SFIXED 32
4 | 6 | 13 => (
"u32".to_string(),
"roto_runtime::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)".to_string(),
), // UINT/FIXED 32
16 | 18 => (
"i64".to_string(),
"roto_runtime::read_varint(bytes).map(|(v, _)| v as i64).map_err(|_| roto_runtime::RotoError::WireFormatViolation)".to_string(),
), // SINT/SFIXED 64
7 | 14 => (
"u64".to_string(),
"roto_runtime::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| roto_runtime::RotoError::WireFormatViolation)".to_string(),
), // UINT/FIXED 64
8 => (
"bool".to_string(),
"roto_runtime::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| roto_runtime::RotoError::WireFormatViolation)".to_string(),
), // TYPE_BOOL
11 | 12 => ("&'a [u8]".to_string(), "Ok(bytes)".to_string()), // MESSAGE/BYTES
_ => ("&'a [u8]".to_string(), "Ok(bytes)".to_string()),
}
}
fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
let enum_name = to_pascal_case(enum_proto.name().unwrap());
output.push_str(&format!(
"#[derive(Debug, Clone, Copy, PartialEq, Eq)]\n#[repr(i32)]\npub enum {} {{\n",
enum_name
));
let mut values = enum_proto.value();
let mut zero_variant_name = None;
while let Some(val_res) = values.next() {
let (val_data, _) = val_res.expect("Failed to iterate enum");
let accessor =
ProtoAccessor::new(val_data).expect("Failed to parse EnumValueDescriptorProto");
let (name_bytes, _) = accessor.get_value(1).expect("Enum value name missing");
let name = str::from_utf8(name_bytes).expect("Enum value name invalid utf8");
let (num_bytes, _) = accessor.get_value(2).expect("Enum value number missing");
let (num, _) =
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
let pascal_name = to_pascal_case(name);
if num == 0 {
zero_variant_name = Some(pascal_name.clone());
}
output.push_str(&format!(" {} = {},\n", pascal_name, num));
}
if zero_variant_name.is_none() {
output.push_str(" Unknown = 0,\n");
zero_variant_name = Some("Unknown".to_string());
}
output.push_str("}\n\n");
output.push_str(&format!(
"impl {} {{\n pub fn from_i32(value: i32) -> Self {{\n match value {{\n",
enum_name
));
let mut values = enum_proto.value();
while let Some(val_res) = values.next() {
let (val_data, _) = val_res.expect("Failed to read enum value");
let accessor =
ProtoAccessor::new(val_data).expect("Failed to parse EnumValueDescriptorProto");
let (name_bytes, _) = accessor.get_value(1).expect("Enum value name missing");
let name = str::from_utf8(name_bytes).expect("Enum value name invalid utf8");
let (num_bytes, _) = accessor.get_value(2).expect("Enum value number missing");
let (num, _) =
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
output.push_str(&format!(
" {} => {}::{},\n",
num,
enum_name,
to_pascal_case(name)
));
}
output.push_str(&format!(
" _ => {}::{},\n",
enum_name,
zero_variant_name.as_ref().unwrap()
));
output.push_str(" }\n }\n}\n\n");
}
fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
let msg_name = to_pascal_case(msg_proto.name().unwrap());
let mut fields_info = Vec::new();
for field_res in msg_proto.field() {
let (field_data, _) = field_res.expect("Failed to iterate field");
let field_proto =
FieldDescriptorProto::new(field_data).expect("Failed to parse FieldDescriptorProto");
let field_name = field_proto.name().unwrap();
let tag = field_proto.number().unwrap();
let f_type = field_proto.r#type().unwrap() as i32;
let f_label = field_proto.label().unwrap() as i32;
fields_info.push((field_name.to_string(), tag, f_type, f_label));
}
output.push_str(&format!("pub struct {}<'a> {{\n", msg_name));
output.push_str(" accessor: roto_runtime::ProtoAccessor<'a>,\n");
for (field_name, _tag, _f_type, f_label) in &fields_info {
if *f_label == 3 {
output.push_str(&format!(" {}_start: Option<usize>,\n", field_name));
output.push_str(&format!(" {}_end: Option<usize>,\n", field_name));
} else {
output.push_str(&format!(" {}_offset: Option<usize>,\n", field_name));
}
}
output.push_str("}\n\n");
output.push_str(&format!("impl<'a> {}<'a> {{\n", msg_name));
output.push_str(" pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {\n");
output.push_str(" let accessor = roto_runtime::ProtoAccessor::new(data)?;\n");
if !fields_info.is_empty() {
for (name, _, _, label) in &fields_info {
if *label == 3 {
output.push_str(&format!(" let mut {}_start = None;\n", name));
output.push_str(&format!(" let mut {}_end = None;\n", name));
} else {
output.push_str(&format!(" let mut {}_offset = None;\n", name));
}
}
output.push_str(" for item in accessor.fields() {\n");
output.push_str(" let (offset, tag, _) = item?;\n");
for (name, tag, _, label) in &fields_info {
if *label == 3 {
output.push_str(&format!(" if tag.field_number == {} {{\n", tag));
output.push_str(&format!(
" if {}_start.is_none() {{ {}_start = Some(offset); }}\n",
name, name
));
output.push_str(&format!(" {}_end = Some(offset);\n", name));
output.push_str(" }\n");
} else {
output.push_str(&format!(
" if tag.field_number == {} {{ {}_offset = Some(offset); }}\n",
tag, name
));
}
}
output.push_str(" }\n\n");
}
output.push_str(" Ok(Self {\n");
output.push_str(" accessor,\n");
for (name, _, _, label) in &fields_info {
if *label == 3 {
output.push_str(&format!("{}_start, {}_end,\n", name, name));
} else {
output.push_str(&format!("{}_offset,\n", name));
}
}
output.push_str(" })\n }\n\n");
for (field_name, tag, f_type, f_label) in fields_info {
let (rust_type, logic) = map_type_to_rust_accessor(f_type, f_label);
let safe_name = if field_name == "type" {
format!("r#{}", field_name)
} else {
field_name.clone()
};
if f_label == 3 {
output.push_str(&format!(
" pub fn {}(&self) -> {} {{\n",
safe_name, rust_type
));
output.push_str(&format!(
" match (self.{}_start, self.{}_end) {{\n",
field_name, field_name
));
output.push_str(&format!(" (Some(start), Some(end)) => self.accessor.iter_repeated_range({}, start, end),\n", tag));
output.push_str(&format!(
" _ => self.accessor.iter_repeated({}),\n",
tag
));
output.push_str(" }\n }\n\n");
} else {
output.push_str(&format!(
" pub fn {}(&self) -> roto_runtime::Result<{}> {{\n",
safe_name, rust_type
));
output.push_str(&format!(
" let offset = self.{}_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;\n",
field_name
));
output.push_str(" let (bytes, _) = self.accessor.get_value_at(offset)?;\n");
output.push_str(&format!(" {}\n", logic));
output.push_str(" }\n\n");
}
}
// raw_fields() convenience on the message struct (before closing the impl)
output.push_str(" pub fn raw_fields(&self) -> roto::RawFieldIterator<'a> {\n");
output.push_str(" self.accessor.raw_fields()\n");
output.push_str(" }\n\n");
output.push_str("}\n\n");
// Collect builder field info so we can use it multiple times below.
// Tuple: (field_name, safe_name, tag, rust_type, write_method)
let mut builder_fields: Vec<(String, String, u32, String, String)> = Vec::new();
for field_res in msg_proto.field() {
let (field_data, _) = field_res.expect("Failed to iterate field");
let field_proto =
FieldDescriptorProto::new(field_data).expect("Failed to parse FieldDescriptorProto");
let field_name = field_proto.name().unwrap().to_string();
let safe_name = if field_name == "type" {
format!("r#{}", field_name)
} else {
field_name.clone()
};
let tag = field_proto.number().unwrap();
let f_type = field_proto.r#type().unwrap() as i32;
let (rust_type, method) = map_type_to_rust_builder(f_type);
builder_fields.push((field_name, safe_name, tag as u32, rust_type, method));
}
// Builder struct — one `_written: bool` flag per field
output.push_str(&format!("pub struct {}Builder<'b> {{\n", msg_name));
output.push_str(" builder: roto_runtime::ProtoBuilder<'b>,\n");
for (field_name, _, _, _, _) in &builder_fields {
output.push_str(&format!(" {}_written: bool,\n", field_name));
}
output.push_str(&format!("}}\n\nimpl<'b> {}Builder<'b> {{\n", msg_name));
// Constructor — initialise every flag to false
output.push_str(&format!(
" pub fn builder(buf: &mut [u8]) -> {}Builder<'_> {{\n {}Builder {{\n",
msg_name, msg_name
));
output.push_str(" builder: roto_runtime::ProtoBuilder::new(buf),\n");
for (field_name, _, _, _, _) in &builder_fields {
output.push_str(&format!(" {}_written: false,\n", field_name));
}
output.push_str(" }\n }\n\n");
// Per-field setters — mark field as written
for (field_name, safe_name, tag, rust_type, method) in &builder_fields {
output.push_str(&format!(
" pub fn {}(mut self, value: {}) -> roto_runtime::Result<Self> {{\n self.builder.{}({}, value)?;\n self.{}_written = true;\n Ok(self)\n }}\n\n",
safe_name, rust_type, method, tag, field_name
));
}
// with() — copies unseen fields from an existing message
output.push_str(&format!(
" pub fn with(mut self, msg: &{}<'_>) -> roto_runtime::Result<Self> {{\n",
msg_name
));
output.push_str(" for item in msg.raw_fields() {\n");
output.push_str(" let (field_number, raw_bytes) = item?;\n");
output.push_str(" let is_written = match field_number {\n");
for (field_name, _, tag, _, _) in &builder_fields {
output.push_str(&format!(
" {} => self.{}_written,\n",
tag, field_name
));
}
output.push_str(" _ => false,\n");
output.push_str(" };\n");
output.push_str(" if !is_written {\n");
output.push_str(" self.builder.write_raw(raw_bytes)?;\n");
output.push_str(" }\n");
output.push_str(" }\n");
output.push_str(" Ok(self)\n");
output.push_str(" }\n\n");
output.push_str(&format!(" pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n"));
let mut nested_enums = Vec::new();
for e_res in msg_proto.enum_type() {
if let Ok((e, _)) = e_res {
nested_enums.push(e);
}
}
let mut nested_msgs = Vec::new();
for m_res in msg_proto.nested_type() {
if let Ok((m, _)) = m_res {
nested_msgs.push(m);
}
}
if !nested_enums.is_empty() || !nested_msgs.is_empty() {
let mod_name = to_snake_case(msg_proto.name().unwrap());
output.push_str(&format!("pub mod {} {{\n", mod_name));
for e_data in nested_enums {
write_enum(
&EnumDescriptorProto::new(e_data)
.expect("Failed to parse nested EnumDescriptorProto"),
output,
);
}
for m_data in nested_msgs {
write_message(
&DescriptorProto::new(m_data).expect("Failed to parse nested DescriptorProto"),
output,
);
}
output.push_str("}\n\n");
}
}
fn map_type_to_rust_builder(field_type: i32) -> (String, String) {
match field_type {
9 => ("&str".to_string(), "write_string".to_string()),
5 | 17 => ("i32".to_string(), "write_int32".to_string()),
3 | 4 | 8 | 13 | 14 | 18 => ("u64".to_string(), "write_varint".to_string()),
7 | 15 => ("u32".to_string(), "write_fixed32".to_string()),
6 | 16 => ("u64".to_string(), "write_fixed64".to_string()),
11 | 12 => ("&[u8]".to_string(), "write_bytes".to_string()),
_ => ("&[u8]".to_string(), "write_bytes".to_string()),
}
}
pub fn generate_rust_code(
set: &FileDescriptorSet,
files_to_generate: Option<&[String]>,
generate_mod_files: bool,
) -> Vec<(String, String)> {
let mut generated_files = Vec::new();
for file_res in set.file() {
let (file_data, _) = file_res.expect("Failed to iterate file");
let file_proto =
FileDescriptorProto::new(file_data).expect("Failed to parse FileDescriptorProto");
let proto_name = file_proto.name().expect("File proto name missing");
if let Some(filter) = files_to_generate {
if !filter.contains(&proto_name.to_string()) {
continue;
}
}
let rust_file_name = format!("{}.rs", proto_name.replace(".proto", ""));
let mut output = String::new();
output.push_str("// @generated by protoc-gen-roto — do not edit\n");
output.push_str("#![allow(unused_imports)]\n\n");
output.push_str("use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};\n");
output.push_str("use std::str;\n\n");
for dep_res in file_proto.dependency() {
let (dep_data, _) = dep_res.expect("Failed to iterate dependency");
let dep_name = str::from_utf8(dep_data).expect("Dependency name invalid utf8");
let dep_mod_path = dep_name.replace(".proto", "").replace('/', "::");
output.push_str(&format!("use crate::{};\n", dep_mod_path));
}
output.push_str("\n");
// Enums
for enum_res in file_proto.enum_type() {
let (enum_data, _) = enum_res.expect("Failed to iterate enum");
write_enum(
&EnumDescriptorProto::new(enum_data).expect("Failed to parse EnumDescriptorProto"),
&mut output,
);
}
// Messages
for msg_res in file_proto.message_type() {
let (msg_data, _) = msg_res.expect("Failed to iterate message");
write_message(
&DescriptorProto::new(msg_data).expect("Failed to parse DescriptorProto"),
&mut output,
);
}
generated_files.push((rust_file_name, output));
}
if !generate_mod_files {
return generated_files;
}
let mut all_paths: Vec<String> = generated_files.iter().map(|(p, _)| p.clone()).collect();
all_paths.sort();
let mut mod_files: HashMap<String, HashSet<String>> = HashMap::new();
for path in &all_paths {
let parts: Vec<&str> = path.split('/').collect();
let mut current_dir = String::new();
for i in 0..parts.len() - 1 {
if !current_dir.is_empty() {
current_dir.push('/');
}
current_dir.push_str(parts[i]);
let mod_path = format!("{}/mod.rs", current_dir);
let sub_mod = parts[i + 1].replace(".rs", "");
mod_files.entry(mod_path).or_default().insert(sub_mod);
}
}
let mut root_mods = HashSet::new();
for path in &all_paths {
let parts: Vec<&str> = path.split('/').collect();
root_mods.insert(parts[0].replace(".rs", ""));
}
let mut root_mod_content = String::new();
root_mod_content.push_str("// @generated by protoc-gen-roto — do not edit\n");
root_mod_content.push_str("#![allow(unused_imports)]\n\n");
let mut sorted_root_mods: Vec<_> = root_mods.into_iter().collect();
sorted_root_mods.sort();
for m in sorted_root_mods {
root_mod_content.push_str(&format!("pub mod {};\n", m));
}
generated_files.push(("mod.rs".to_string(), root_mod_content));
for (mod_path, sub_mods) in mod_files {
let mut content = String::new();
content.push_str("// @generated by protoc-gen-roto — do not edit\n");
content.push_str("#![allow(unused_imports)]\n\n");
let mut sorted_subs: Vec<_> = sub_mods.into_iter().collect();
sorted_subs.sort();
for sub in sorted_subs {
content.push_str(&format!("pub mod {};\n", sub));
}
generated_files.push((mod_path, content));
}
generated_files
}
+1
View File
@@ -0,0 +1 @@
pub mod protobuf;
@@ -0,0 +1 @@
pub mod plugin;
@@ -0,0 +1,679 @@
// @generated by protoc-gen-roto — do not edit
#![allow(unused_imports)]
use roto_runtime::{
ProtoAccessor, ProtoBuilder, RepeatedFieldIterator, Result, RotoError, read_varint,
};
use std::str;
use crate::google::protobuf::descriptor;
pub struct Version<'a> {
accessor: roto_runtime::ProtoAccessor<'a>,
major_offset: Option<usize>,
minor_offset: Option<usize>,
patch_offset: Option<usize>,
suffix_offset: Option<usize>,
}
impl<'a> Version<'a> {
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
let accessor = roto_runtime::ProtoAccessor::new(data)?;
let mut major_offset = None;
let mut minor_offset = None;
let mut patch_offset = None;
let mut suffix_offset = None;
for item in accessor.fields() {
let (offset, tag, _) = item?;
if tag.field_number == 1 {
major_offset = Some(offset);
}
if tag.field_number == 2 {
minor_offset = Some(offset);
}
if tag.field_number == 3 {
patch_offset = Some(offset);
}
if tag.field_number == 4 {
suffix_offset = Some(offset);
}
}
Ok(Self {
accessor,
major_offset,
minor_offset,
patch_offset,
suffix_offset,
})
}
pub fn major(&self) -> roto_runtime::Result<i32> {
let offset = self
.major_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
roto_runtime::read_varint(bytes)
.map(|(v, _)| v as i32)
.map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn minor(&self) -> roto_runtime::Result<i32> {
let offset = self
.minor_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
roto_runtime::read_varint(bytes)
.map(|(v, _)| v as i32)
.map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn patch(&self) -> roto_runtime::Result<i32> {
let offset = self
.patch_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
roto_runtime::read_varint(bytes)
.map(|(v, _)| v as i32)
.map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn suffix(&self) -> roto_runtime::Result<&'a str> {
let offset = self
.suffix_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
self.accessor.raw_fields()
}
}
pub struct VersionBuilder<'b> {
builder: roto_runtime::ProtoBuilder<'b>,
major_written: bool,
minor_written: bool,
patch_written: bool,
suffix_written: bool,
}
impl<'b> VersionBuilder<'b> {
pub fn builder(buf: &mut [u8]) -> VersionBuilder<'_> {
VersionBuilder {
builder: roto_runtime::ProtoBuilder::new(buf),
major_written: false,
minor_written: false,
patch_written: false,
suffix_written: false,
}
}
pub fn major(mut self, value: i32) -> roto_runtime::Result<Self> {
self.builder.write_int32(1, value)?;
self.major_written = true;
Ok(self)
}
pub fn minor(mut self, value: i32) -> roto_runtime::Result<Self> {
self.builder.write_int32(2, value)?;
self.minor_written = true;
Ok(self)
}
pub fn patch(mut self, value: i32) -> roto_runtime::Result<Self> {
self.builder.write_int32(3, value)?;
self.patch_written = true;
Ok(self)
}
pub fn suffix(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(4, value)?;
self.suffix_written = true;
Ok(self)
}
pub fn with(mut self, msg: &Version<'_>) -> roto_runtime::Result<Self> {
for item in msg.raw_fields() {
let (field_number, raw_bytes) = item?;
let is_written = match field_number {
1 => self.major_written,
2 => self.minor_written,
3 => self.patch_written,
4 => self.suffix_written,
_ => false,
};
if !is_written {
self.builder.write_raw(raw_bytes)?;
}
}
Ok(self)
}
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
self.builder.finish()
}
}
pub struct CodeGeneratorRequest<'a> {
accessor: roto_runtime::ProtoAccessor<'a>,
file_to_generate_start: Option<usize>,
file_to_generate_end: Option<usize>,
parameter_offset: Option<usize>,
proto_file_start: Option<usize>,
proto_file_end: Option<usize>,
source_file_descriptors_start: Option<usize>,
source_file_descriptors_end: Option<usize>,
compiler_version_offset: Option<usize>,
}
impl<'a> CodeGeneratorRequest<'a> {
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
let accessor = roto_runtime::ProtoAccessor::new(data)?;
let mut file_to_generate_start = None;
let mut file_to_generate_end = None;
let mut parameter_offset = None;
let mut proto_file_start = None;
let mut proto_file_end = None;
let mut source_file_descriptors_start = None;
let mut source_file_descriptors_end = None;
let mut compiler_version_offset = None;
for item in accessor.fields() {
let (offset, tag, _) = item?;
if tag.field_number == 1 {
if file_to_generate_start.is_none() {
file_to_generate_start = Some(offset);
}
file_to_generate_end = Some(offset);
}
if tag.field_number == 2 {
parameter_offset = Some(offset);
}
if tag.field_number == 15 {
if proto_file_start.is_none() {
proto_file_start = Some(offset);
}
proto_file_end = Some(offset);
}
if tag.field_number == 17 {
if source_file_descriptors_start.is_none() {
source_file_descriptors_start = Some(offset);
}
source_file_descriptors_end = Some(offset);
}
if tag.field_number == 3 {
compiler_version_offset = Some(offset);
}
}
Ok(Self {
accessor,
file_to_generate_start,
file_to_generate_end,
parameter_offset,
proto_file_start,
proto_file_end,
source_file_descriptors_start,
source_file_descriptors_end,
compiler_version_offset,
})
}
pub fn file_to_generate(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
match (self.file_to_generate_start, self.file_to_generate_end) {
(Some(start), Some(end)) => self.accessor.iter_repeated_range(1, start, end),
_ => self.accessor.iter_repeated(1),
}
}
pub fn parameter(&self) -> roto_runtime::Result<&'a str> {
let offset = self
.parameter_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn proto_file(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
match (self.proto_file_start, self.proto_file_end) {
(Some(start), Some(end)) => self.accessor.iter_repeated_range(15, start, end),
_ => self.accessor.iter_repeated(15),
}
}
pub fn source_file_descriptors(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
match (
self.source_file_descriptors_start,
self.source_file_descriptors_end,
) {
(Some(start), Some(end)) => self.accessor.iter_repeated_range(17, start, end),
_ => self.accessor.iter_repeated(17),
}
}
pub fn compiler_version(&self) -> roto_runtime::Result<&'a [u8]> {
let offset = self
.compiler_version_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
Ok(bytes)
}
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
self.accessor.raw_fields()
}
}
pub struct CodeGeneratorRequestBuilder<'b> {
builder: roto_runtime::ProtoBuilder<'b>,
file_to_generate_written: bool,
parameter_written: bool,
proto_file_written: bool,
source_file_descriptors_written: bool,
compiler_version_written: bool,
}
impl<'b> CodeGeneratorRequestBuilder<'b> {
pub fn builder(buf: &mut [u8]) -> CodeGeneratorRequestBuilder<'_> {
CodeGeneratorRequestBuilder {
builder: roto_runtime::ProtoBuilder::new(buf),
file_to_generate_written: false,
parameter_written: false,
proto_file_written: false,
source_file_descriptors_written: false,
compiler_version_written: false,
}
}
pub fn file_to_generate(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(1, value)?;
self.file_to_generate_written = true;
Ok(self)
}
pub fn parameter(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(2, value)?;
self.parameter_written = true;
Ok(self)
}
pub fn proto_file(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
self.builder.write_bytes(15, value)?;
self.proto_file_written = true;
Ok(self)
}
pub fn source_file_descriptors(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
self.builder.write_bytes(17, value)?;
self.source_file_descriptors_written = true;
Ok(self)
}
pub fn compiler_version(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
self.builder.write_bytes(3, value)?;
self.compiler_version_written = true;
Ok(self)
}
pub fn with(mut self, msg: &CodeGeneratorRequest<'_>) -> roto_runtime::Result<Self> {
for item in msg.raw_fields() {
let (field_number, raw_bytes) = item?;
let is_written = match field_number {
1 => self.file_to_generate_written,
2 => self.parameter_written,
15 => self.proto_file_written,
17 => self.source_file_descriptors_written,
3 => self.compiler_version_written,
_ => false,
};
if !is_written {
self.builder.write_raw(raw_bytes)?;
}
}
Ok(self)
}
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
self.builder.finish()
}
}
pub struct CodeGeneratorResponse<'a> {
accessor: roto_runtime::ProtoAccessor<'a>,
error_offset: Option<usize>,
supported_features_offset: Option<usize>,
minimum_edition_offset: Option<usize>,
maximum_edition_offset: Option<usize>,
file_start: Option<usize>,
file_end: Option<usize>,
}
impl<'a> CodeGeneratorResponse<'a> {
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
let accessor = roto_runtime::ProtoAccessor::new(data)?;
let mut error_offset = None;
let mut supported_features_offset = None;
let mut minimum_edition_offset = None;
let mut maximum_edition_offset = None;
let mut file_start = None;
let mut file_end = None;
for item in accessor.fields() {
let (offset, tag, _) = item?;
if tag.field_number == 1 {
error_offset = Some(offset);
}
if tag.field_number == 2 {
supported_features_offset = Some(offset);
}
if tag.field_number == 3 {
minimum_edition_offset = Some(offset);
}
if tag.field_number == 4 {
maximum_edition_offset = Some(offset);
}
if tag.field_number == 15 {
if file_start.is_none() {
file_start = Some(offset);
}
file_end = Some(offset);
}
}
Ok(Self {
accessor,
error_offset,
supported_features_offset,
minimum_edition_offset,
maximum_edition_offset,
file_start,
file_end,
})
}
pub fn error(&self) -> roto_runtime::Result<&'a str> {
let offset = self
.error_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn supported_features(&self) -> roto_runtime::Result<u32> {
let offset = self
.supported_features_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
roto_runtime::read_varint(bytes)
.map(|(v, _)| v as u32)
.map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn minimum_edition(&self) -> roto_runtime::Result<i32> {
let offset = self
.minimum_edition_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
roto_runtime::read_varint(bytes)
.map(|(v, _)| v as i32)
.map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn maximum_edition(&self) -> roto_runtime::Result<i32> {
let offset = self
.maximum_edition_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
roto_runtime::read_varint(bytes)
.map(|(v, _)| v as i32)
.map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn file(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
match (self.file_start, self.file_end) {
(Some(start), Some(end)) => self.accessor.iter_repeated_range(15, start, end),
_ => self.accessor.iter_repeated(15),
}
}
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
self.accessor.raw_fields()
}
}
pub struct CodeGeneratorResponseBuilder<'b> {
builder: roto_runtime::ProtoBuilder<'b>,
error_written: bool,
supported_features_written: bool,
minimum_edition_written: bool,
maximum_edition_written: bool,
file_written: bool,
}
impl<'b> CodeGeneratorResponseBuilder<'b> {
pub fn builder(buf: &mut [u8]) -> CodeGeneratorResponseBuilder<'_> {
CodeGeneratorResponseBuilder {
builder: roto_runtime::ProtoBuilder::new(buf),
error_written: false,
supported_features_written: false,
minimum_edition_written: false,
maximum_edition_written: false,
file_written: false,
}
}
pub fn error(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(1, value)?;
self.error_written = true;
Ok(self)
}
pub fn supported_features(mut self, value: u64) -> roto_runtime::Result<Self> {
self.builder.write_varint(2, value)?;
self.supported_features_written = true;
Ok(self)
}
pub fn minimum_edition(mut self, value: i32) -> roto_runtime::Result<Self> {
self.builder.write_int32(3, value)?;
self.minimum_edition_written = true;
Ok(self)
}
pub fn maximum_edition(mut self, value: i32) -> roto_runtime::Result<Self> {
self.builder.write_int32(4, value)?;
self.maximum_edition_written = true;
Ok(self)
}
pub fn file(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
self.builder.write_bytes(15, value)?;
self.file_written = true;
Ok(self)
}
pub fn with(mut self, msg: &CodeGeneratorResponse<'_>) -> roto_runtime::Result<Self> {
for item in msg.raw_fields() {
let (field_number, raw_bytes) = item?;
let is_written = match field_number {
1 => self.error_written,
2 => self.supported_features_written,
3 => self.minimum_edition_written,
4 => self.maximum_edition_written,
15 => self.file_written,
_ => false,
};
if !is_written {
self.builder.write_raw(raw_bytes)?;
}
}
Ok(self)
}
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
self.builder.finish()
}
}
pub mod code_generator_response {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum Feature {
FEATURENONE = 0,
FEATUREPROTO3OPTIONAL = 1,
FEATURESUPPORTSEDITIONS = 2,
}
impl Feature {
pub fn from_i32(value: i32) -> Self {
match value {
0 => Feature::FEATURENONE,
1 => Feature::FEATUREPROTO3OPTIONAL,
2 => Feature::FEATURESUPPORTSEDITIONS,
_ => Feature::FEATURENONE,
}
}
}
pub struct File<'a> {
accessor: roto_runtime::ProtoAccessor<'a>,
name_offset: Option<usize>,
insertion_point_offset: Option<usize>,
content_offset: Option<usize>,
generated_code_info_offset: Option<usize>,
}
impl<'a> File<'a> {
pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
let accessor = roto_runtime::ProtoAccessor::new(data)?;
let mut name_offset = None;
let mut insertion_point_offset = None;
let mut content_offset = None;
let mut generated_code_info_offset = None;
for item in accessor.fields() {
let (offset, tag, _) = item?;
if tag.field_number == 1 {
name_offset = Some(offset);
}
if tag.field_number == 2 {
insertion_point_offset = Some(offset);
}
if tag.field_number == 15 {
content_offset = Some(offset);
}
if tag.field_number == 16 {
generated_code_info_offset = Some(offset);
}
}
Ok(Self {
accessor,
name_offset,
insertion_point_offset,
content_offset,
generated_code_info_offset,
})
}
pub fn name(&self) -> roto_runtime::Result<&'a str> {
let offset = self
.name_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn insertion_point(&self) -> roto_runtime::Result<&'a str> {
let offset = self
.insertion_point_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn content(&self) -> roto_runtime::Result<&'a str> {
let offset = self
.content_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
}
pub fn generated_code_info(&self) -> roto_runtime::Result<&'a [u8]> {
let offset = self
.generated_code_info_offset
.ok_or(roto_runtime::RotoError::FieldNotFound)?;
let (bytes, _) = self.accessor.get_value_at(offset)?;
Ok(bytes)
}
pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
self.accessor.raw_fields()
}
}
pub struct FileBuilder<'b> {
builder: roto_runtime::ProtoBuilder<'b>,
name_written: bool,
insertion_point_written: bool,
content_written: bool,
generated_code_info_written: bool,
}
impl<'b> FileBuilder<'b> {
pub fn builder(buf: &mut [u8]) -> FileBuilder<'_> {
FileBuilder {
builder: roto_runtime::ProtoBuilder::new(buf),
name_written: false,
insertion_point_written: false,
content_written: false,
generated_code_info_written: false,
}
}
pub fn name(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(1, value)?;
self.name_written = true;
Ok(self)
}
pub fn insertion_point(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(2, value)?;
self.insertion_point_written = true;
Ok(self)
}
pub fn content(mut self, value: &str) -> roto_runtime::Result<Self> {
self.builder.write_string(15, value)?;
self.content_written = true;
Ok(self)
}
pub fn generated_code_info(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
self.builder.write_bytes(16, value)?;
self.generated_code_info_written = true;
Ok(self)
}
pub fn with(mut self, msg: &File<'_>) -> roto_runtime::Result<Self> {
for item in msg.raw_fields() {
let (field_number, raw_bytes) = item?;
let is_written = match field_number {
1 => self.name_written,
2 => self.insertion_point_written,
15 => self.content_written,
16 => self.generated_code_info_written,
_ => false,
};
if !is_written {
self.builder.write_raw(raw_bytes)?;
}
}
Ok(self)
}
pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
self.builder.finish()
}
}
}
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
pub mod compiler;
pub mod descriptor;
+2
View File
@@ -0,0 +1,2 @@
pub mod generator;
pub mod google;
+89
View File
@@ -0,0 +1,89 @@
use roto_codegen::google::protobuf::compiler::plugin::CodeGeneratorRequest;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
use std::fs;
use std::process::Command;
#[test]
fn test_generated_code_builds() {
// 1. Generate Rust code from data/request.bin
let request_path = "data/request.bin";
let data = fs::read(request_path).expect("Failed to read request.bin");
let request = CodeGeneratorRequest::new(&data).expect("Failed to parse CodeGeneratorRequest");
// Mimic the logic from protoc-gen-roto to build a FileDescriptorSet
let mut set_buf = Vec::new();
for file_res in request.proto_file() {
let (file_data, _) = file_res.expect("Failed to iterate proto_file");
// Tag 1, Length-delimited: (1 << 3) | 2 = 10
set_buf.push(10);
// Write length as varint
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size =
roto_runtime::write_varint(len, &mut len_buf).expect("Failed to write varint length");
set_buf.extend_from_slice(&len_buf[..len_size]);
// Write data
set_buf.extend_from_slice(file_data);
}
let set = FileDescriptorSet::new(&set_buf).expect("Failed to create FileDescriptorSet");
let generated_files = roto_codegen::generator::generate_rust_code(&set, None, false);
assert!(
!generated_files.is_empty(),
"Generated code should not be empty"
);
// 2. Setup a temporary Cargo project to verify the code builds
let root = std::env::current_dir().expect("Failed to get current directory");
let temp_project_dir = root.join("test_gen_project");
// Clean up previous runs
if temp_project_dir.exists() {
fs::remove_dir_all(&temp_project_dir).expect("Failed to clean up temp project directory");
}
// Create new library project
let status = Command::new("cargo")
.args(["new", "--lib", "test_gen_project"])
.current_dir(&root)
.status()
.expect("Failed to run cargo new");
assert!(status.success(), "cargo new failed");
// 3. Configure the project to depend on the current roto crate
let cargo_toml_path = temp_project_dir.join("Cargo.toml");
let cargo_toml_content =
fs::read_to_string(&cargo_toml_path).expect("Failed to read Cargo.toml");
let updated_cargo_toml = format!(
"{}\n\nroto-codegen = {{ path = \"..\" }}\nroto-runtime = {{ path = \"../../runtime\" }}\n\n[workspace]\n",
cargo_toml_content
);
fs::write(cargo_toml_path, updated_cargo_toml).expect("Failed to write Cargo.toml");
// 4. Write the generated code to src/lib.rs
// The generated code uses `use crate::{...}`, but it's now in a separate crate.
// Replace `crate` with `roto` to reference the types in the dependency.
let mut all_code = String::new();
for (_, content) in generated_files {
all_code.push_str(&content);
all_code.push_str("\n");
}
let final_code = all_code.replace("use crate::", "use roto::");
let lib_path = temp_project_dir.join("src/lib.rs");
fs::write(lib_path, final_code).expect("Failed to write generated code to src/lib.rs");
// 5. Attempt to build the project
let build_status = Command::new("cargo")
.args(["build"])
.current_dir(&temp_project_dir)
.status()
.expect("Failed to run cargo build");
assert!(
build_status.success(),
"The generated Rust code failed to build in a standalone project!"
);
}
+55
View File
@@ -0,0 +1,55 @@
use roto_codegen::generator::generate_rust_code;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
use std::fs;
#[test]
fn test_nested_proto_generation_contains_modules() {
let request_path = "data/request.bin";
if !std::path::Path::new(request_path).exists() {
panic!("data/request.bin not found. This test requires the sample request binary.");
}
let data = fs::read(request_path).expect("Failed to read request.bin");
// The existing test logic to build a FileDescriptorSet from CodeGeneratorRequest
// We can simplify this by just wrapping the data if it's already a FileDescriptorSet,
// but request.bin is usually a CodeGeneratorRequest.
// Let's use the same logic as build_generated_code.rs to get a FileDescriptorSet
let request =
roto_codegen::google::protobuf::compiler::plugin::CodeGeneratorRequest::new(&data)
.expect("Failed to parse CodeGeneratorRequest");
let mut set_buf = Vec::new();
for file_res in request.proto_file() {
let (file_data, _) = file_res.expect("Failed to iterate proto_file");
set_buf.push(10);
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size =
roto_runtime::write_varint(len, &mut len_buf).expect("Failed to write varint length");
set_buf.extend_from_slice(&len_buf[..len_size]);
set_buf.extend_from_slice(file_data);
}
let set = FileDescriptorSet::new(&set_buf).expect("Failed to create FileDescriptorSet");
let generated_files = generate_rust_code(&set, None, false);
let all_code: String = generated_files
.into_iter()
.map(|(_, content)| content)
.collect();
println!("Generated Code:\n{}", all_code);
// We want to see if any message has a nested module.
// Since we don't know exactly what's in request.bin, we'll look for ANY 'pub mod' inside the generated code
// that isn't at the top level (though the generator puts them inside the message definition).
assert!(
all_code.contains("pub mod "),
"Generated code should contain at least one nested module for nested types"
);
assert!(
all_code.contains("pub struct "),
"Generated code should contain structs"
);
}
+80
View File
@@ -0,0 +1,80 @@
use roto_codegen::generator::generate_rust_code;
use roto_codegen::google::protobuf::compiler::plugin::CodeGeneratorRequest;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
use std::fs;
fn load_generated_code() -> String {
let data = fs::read("data/request.bin").expect("Failed to read data/request.bin");
let request = CodeGeneratorRequest::new(&data).expect("Failed to parse CodeGeneratorRequest");
let mut set_buf = Vec::new();
for file_res in request.proto_file() {
let (file_data, _) = file_res.expect("Failed to iterate proto_file");
set_buf.push(10u8);
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size = roto_runtime::write_varint(len, &mut len_buf).unwrap();
set_buf.extend_from_slice(&len_buf[..len_size]);
set_buf.extend_from_slice(file_data);
}
let set = FileDescriptorSet::new(&set_buf).expect("Failed to create FileDescriptorSet");
generate_rust_code(&set, None, false)
.into_iter()
.map(|(_, content)| content)
.collect()
}
#[test]
fn test_builder_structs_have_written_flags() {
let code = load_generated_code();
assert!(
code.contains("_written: bool"),
"Builder structs should contain `_written: bool` fields for each proto field"
);
}
#[test]
fn test_builder_constructor_initialises_written_flags_to_false() {
let code = load_generated_code();
assert!(
code.contains("_written: false"),
"Builder constructors should initialise every `_written` flag to false"
);
}
#[test]
fn test_builder_setters_mark_field_as_written() {
let code = load_generated_code();
assert!(
code.contains("_written = true"),
"Each builder setter should set its `_written` flag to true"
);
}
#[test]
fn test_builder_has_with_method() {
let code = load_generated_code();
assert!(
code.contains("pub fn with("),
"Each builder impl should expose a `with` method"
);
}
#[test]
fn test_message_structs_have_raw_fields_method() {
let code = load_generated_code();
assert!(
code.contains("pub fn raw_fields("),
"Each message struct impl should expose a `raw_fields` method"
);
}
#[test]
fn test_with_method_uses_write_raw() {
let code = load_generated_code();
assert!(
code.contains("write_raw(raw_bytes)"),
"The `with` method should call `write_raw` to copy field bytes"
);
}