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
Generated
+20 -1
View File
@@ -478,6 +478,10 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "protos"
version = "0.1.0"
[[package]]
name = "quote"
version = "1.0.45"
@@ -537,15 +541,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
[[package]]
name = "roto"
name = "roto-benches"
version = "0.1.0"
dependencies = [
"clap",
"criterion",
"env_logger",
"log",
"roto-runtime",
]
[[package]]
name = "roto-codegen"
version = "0.1.0"
dependencies = [
"clap",
"env_logger",
"log",
"roto-runtime",
]
[[package]]
name = "roto-runtime"
version = "0.1.0"
[[package]]
name = "rustversion"
version = "1.0.22"
+10 -15
View File
@@ -1,16 +1,11 @@
[package]
name = "roto"
version = "0.1.0"
edition = "2024"
[workspace]
members = [
"runtime",
"codegen",
"protos",
"benches",
]
[dependencies]
clap = { version = "4", features = ["derive"] }
log = "0.4"
env_logger = "0.11"
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "hackers_bench"
harness = false
exclude = [
"test_gen_project"
]
+1
View File
@@ -0,0 +1 @@
data
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "roto-benches"
version = "0.1.0"
edition = "2024"
[dependencies]
roto-runtime = { path = "../runtime" }
clap = { version = "4", features = ["derive"] }
log = "0.4"
env_logger = "0.11"
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
[[bench]]
name = "hackers_bench"
harness = false
@@ -27,7 +27,7 @@
//! - `iterate` — counting repeated fields at different nesting depths
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use roto::hackers::{Campaign, Hacker, Operation, Worm};
use roto_benches::hackers::{Campaign, Hacker, Operation, Worm};
use std::hint::black_box;
// =============================================================================
@@ -17,7 +17,7 @@
//! Files land in `data/bench/` by default.
use clap::Parser;
use roto::hackers::{
use roto_benches::hackers::{
CampaignBuilder, ConnectionBuilder, HackerBuilder, OperationBuilder, ToolBuilder, WormBuilder,
};
use std::io::{self, Write};
@@ -449,10 +449,11 @@ fn main() {
);
// Default output: data/bench/<preset>.pb, or stdout if no output and no preset
let out_path = args
.output
.clone()
.or_else(|| args.preset.as_ref().map(|p| format!("data/bench/{}.pb", p)));
let out_path = args.output.clone().or_else(|| {
args.preset
.as_ref()
.map(|p| format!("benches/data/bench/{}.pb", p))
});
match out_path {
Some(ref path) => {
File diff suppressed because it is too large Load Diff
+1
View File
@@ -0,0 +1 @@
pub mod hackers;
+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"
@@ -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;
}
@@ -1,13 +1,15 @@
use clap::Parser;
use roto::google::protobuf::descriptor::{
FileDescriptorSet
};
use roto::generator::generate_rust_code;
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")]
#[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)]
@@ -1,13 +1,11 @@
use env_logger::init;
use log::{error, info};
use roto::generator::generate_rust_code;
use roto::google::protobuf::descriptor::{
FileDescriptorSet
};
use roto::google::protobuf::compiler::plugin::{
use roto_codegen::generator::generate_rust_code;
use roto_codegen::google::protobuf::compiler::plugin::{
CodeGeneratorRequest, CodeGeneratorResponseBuilder, code_generator_response::FileBuilder,
};
// use roto::ProtoBuilder;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
// use roto_runtime::ProtoBuilder;
use std::io::{self, Read, Write};
fn main() {
@@ -43,7 +41,9 @@ fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
}
/// 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>> {
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() {
@@ -58,7 +58,7 @@ fn handle_request(request: &CodeGeneratorRequest) -> std::result::Result<Vec<u8>
// Write length as varint
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size = roto::write_varint(len, &mut len_buf).map_err(|e| {
let len_size = roto_runtime::write_varint(len, &mut len_buf).map_err(|e| {
error!("Failed to write varint length: {:?}", e);
e
})?;
@@ -70,7 +70,8 @@ fn handle_request(request: &CodeGeneratorRequest) -> std::result::Result<Vec<u8>
let set = FileDescriptorSet::new(&set_buf)?;
let files_to_generate: Vec<String> = request.file_to_generate()
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())
@@ -98,9 +99,7 @@ fn handle_request(request: &CodeGeneratorRequest) -> std::result::Result<Vec<u8>
resp_builder = resp_builder.file(final_file)?;
}
let final_response_slice = resp_builder
.finish()
.map_err(|e| {
let final_response_slice = resp_builder.finish().map_err(|e| {
error!("Failed to finish CodeGeneratorResponse: {:?}", e);
e
})?;
@@ -125,12 +124,19 @@ mod tests {
}
let data = fs::read(request_path).expect("Failed to read request.bin");
let request = CodeGeneratorRequest::new(&data).expect("Failed to parse CodeGeneratorRequest");
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");
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");
assert!(
!response.is_empty(),
"The generated response should not be empty"
);
}
}
+22 -20
View File
@@ -1,8 +1,8 @@
use crate::ProtoAccessor;
use crate::google::protobuf::descriptor::{
DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto,
FileDescriptorSet,
};
use roto_runtime::ProtoAccessor;
use std::collections::{HashMap, HashSet};
use std::str;
@@ -37,7 +37,7 @@ fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) {
if label == 3 {
// LABEL_REPEATED
return (
"crate::RepeatedFieldIterator<'a>".to_string(),
"roto_runtime::RepeatedFieldIterator<'a>".to_string(),
"".to_string(), // Not used for repeated fields in the same way
);
}
@@ -57,23 +57,23 @@ fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) {
), // TYPE_FLOAT
3 | 5 | 15 | 17 => (
"i32".to_string(),
"crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| crate::RotoError::WireFormatViolation)".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(),
"crate::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| crate::RotoError::WireFormatViolation)".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(),
"crate::read_varint(bytes).map(|(v, _)| v as i64).map_err(|_| crate::RotoError::WireFormatViolation)".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(),
"crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| crate::RotoError::WireFormatViolation)".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(),
"crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| crate::RotoError::WireFormatViolation)".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()),
@@ -97,7 +97,8 @@ fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
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, _) = crate::read_varint(num_bytes).expect("Enum value number invalid varint");
let (num, _) =
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
let pascal_name = to_pascal_case(name);
if num == 0 {
@@ -126,7 +127,8 @@ fn write_enum(enum_proto: &EnumDescriptorProto, output: &mut String) {
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, _) = crate::read_varint(num_bytes).expect("Enum value number invalid varint");
let (num, _) =
roto_runtime::read_varint(num_bytes).expect("Enum value number invalid varint");
output.push_str(&format!(
" {} => {}::{},\n",
@@ -162,7 +164,7 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
}
output.push_str(&format!("pub struct {}<'a> {{\n", msg_name));
output.push_str(" accessor: crate::ProtoAccessor<'a>,\n");
output.push_str(" accessor: roto_runtime::ProtoAccessor<'a>,\n");
for (field_name, _tag, _f_type, f_label) in &fields_info {
if *f_label == 3 {
@@ -175,8 +177,8 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
output.push_str("}\n\n");
output.push_str(&format!("impl<'a> {}<'a> {{\n", msg_name));
output.push_str(" pub fn new(data: &'a [u8]) -> crate::Result<Self> {\n");
output.push_str(" let accessor = crate::ProtoAccessor::new(data)?;\n");
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 {
@@ -245,11 +247,11 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
output.push_str(" }\n }\n\n");
} else {
output.push_str(&format!(
" pub fn {}(&self) -> crate::Result<{}> {{\n",
" pub fn {}(&self) -> roto_runtime::Result<{}> {{\n",
safe_name, rust_type
));
output.push_str(&format!(
" let offset = self.{}_offset.ok_or(crate::RotoError::FieldNotFound)?;\n",
" 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");
@@ -284,7 +286,7 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
// Builder struct — one `_written: bool` flag per field
output.push_str(&format!("pub struct {}Builder<'b> {{\n", msg_name));
output.push_str(" builder: crate::ProtoBuilder<'b>,\n");
output.push_str(" builder: roto_runtime::ProtoBuilder<'b>,\n");
for (field_name, _, _, _, _) in &builder_fields {
output.push_str(&format!(" {}_written: bool,\n", field_name));
}
@@ -295,7 +297,7 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
" pub fn builder(buf: &mut [u8]) -> {}Builder<'_> {{\n {}Builder {{\n",
msg_name, msg_name
));
output.push_str(" builder: crate::ProtoBuilder::new(buf),\n");
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));
}
@@ -304,14 +306,14 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
// 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: {}) -> crate::Result<Self> {{\n self.builder.{}({}, value)?;\n self.{}_written = true;\n Ok(self)\n }}\n\n",
" 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: &{}<'_>) -> crate::Result<Self> {{\n",
" pub fn with(mut self, msg: &{}<'_>) -> roto_runtime::Result<Self> {{\n",
msg_name
));
output.push_str(" for item in msg.raw_fields() {\n");
@@ -332,7 +334,7 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) {
output.push_str(" Ok(self)\n");
output.push_str(" }\n\n");
output.push_str(&format!(" pub fn finish(self) -> crate::Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\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() {
@@ -403,7 +405,7 @@ pub fn generate_rust_code(
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 crate::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};\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() {
+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;
@@ -1,19 +1,14 @@
use roto_codegen::google::protobuf::compiler::plugin::CodeGeneratorRequest;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
use std::fs;
use std::process::Command;
use roto::google::protobuf::descriptor::{
FileDescriptorSet
};
use roto::google::protobuf::compiler::plugin::{
CodeGeneratorRequest,
};
#[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");
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();
@@ -26,17 +21,20 @@ fn test_generated_code_builds() {
// Write length as varint
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size = roto::write_varint(len, &mut len_buf).expect("Failed to write varint length");
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 set = FileDescriptorSet::new(&set_buf).expect("Failed to create FileDescriptorSet");
let generated_files = roto::generator::generate_rust_code(&set, None, false);
assert!(!generated_files.is_empty(), "Generated code should not be empty");
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");
@@ -57,9 +55,10 @@ fn test_generated_code_builds() {
// 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 cargo_toml_content =
fs::read_to_string(&cargo_toml_path).expect("Failed to read Cargo.toml");
let updated_cargo_toml = format!(
"{}\n\nroto = {{ path = \"..\" }}",
"{}\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");
@@ -83,5 +82,8 @@ fn test_generated_code_builds() {
.status()
.expect("Failed to run cargo build");
assert!(build_status.success(), "The generated Rust code failed to build in a standalone project!");
assert!(
build_status.success(),
"The generated Rust code failed to build in a standalone project!"
);
}
@@ -1,7 +1,5 @@
use roto::generator::generate_rust_code;
use roto::google::protobuf::descriptor::{
FileDescriptorSet
};
use roto_codegen::generator::generate_rust_code;
use roto_codegen::google::protobuf::descriptor::FileDescriptorSet;
use std::fs;
#[test]
@@ -18,7 +16,8 @@ fn test_nested_proto_generation_contains_modules() {
// 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::google::protobuf::compiler::plugin::CodeGeneratorRequest::new(&data)
let request =
roto_codegen::google::protobuf::compiler::plugin::CodeGeneratorRequest::new(&data)
.expect("Failed to parse CodeGeneratorRequest");
let mut set_buf = Vec::new();
@@ -27,7 +26,8 @@ fn test_nested_proto_generation_contains_modules() {
set_buf.push(10);
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size = roto::write_varint(len, &mut len_buf).expect("Failed to write varint length");
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);
}
@@ -35,12 +35,21 @@ fn test_nested_proto_generation_contains_modules() {
let generated_files = generate_rust_code(&set, None, false);
let all_code: String = generated_files.into_iter().map(|(_, content)| content).collect();
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");
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"
);
}
@@ -1,6 +1,6 @@
use roto::generator::generate_rust_code;
use roto::google::protobuf::compiler::plugin::CodeGeneratorRequest;
use roto::google::protobuf::descriptor::FileDescriptorSet;
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 {
@@ -13,7 +13,7 @@ fn load_generated_code() -> String {
set_buf.push(10u8);
let len = file_data.len() as u64;
let mut len_buf = [0u8; 10];
let len_size = roto::write_varint(len, &mut len_buf).unwrap();
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);
}
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "protos"
version = "0.1.0"
edition = "2024"
[dependencies]
+3
View File
@@ -0,0 +1,3 @@
pub fn hello() {
println!("Hello from protos!");
}
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "roto-runtime"
version = "0.1.0"
edition = "2024"
[dependencies]
+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;
}
-7
View File
@@ -1,10 +1,3 @@
pub mod generator;
pub mod google;
pub mod hackers;
// Uncomment this to check if the code compiles
// #[path = "../proto/google/protobuf/descriptor.rs"]
// pub mod descriptor;
use std::fmt;
#[derive(Debug, PartialEq, Eq)]
-3
View File
@@ -1,3 +0,0 @@
fn main() {
println!("Hello, world!");
}