From 8ae8c5dfd39b33e880e4e7c9dbac973bf5cdc237 Mon Sep 17 00:00:00 2001 From: charles Date: Sat, 2 May 2026 22:48:03 -0700 Subject: [PATCH] First pass at plugin gen --- Cargo.lock | 134 ++ Cargo.toml | 2 + src/bin/generator.rs | 206 +- src/bin/protoc-gen-roto.rs | 107 + src/generator.rs | 210 ++ src/lib.rs | 1 + src/proto_gen/google/protobuf/descriptor.rs | 278 +++ .../google/protobuf/descriptor_gen.rs | 2030 +++++++++++++++++ 8 files changed, 2765 insertions(+), 203 deletions(-) create mode 100644 src/bin/protoc-gen-roto.rs create mode 100644 src/generator.rs create mode 100644 src/proto_gen/google/protobuf/descriptor_gen.rs diff --git a/Cargo.lock b/Cargo.lock index 59c303c..85c83b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "anstream" version = "1.0.0" @@ -98,6 +107,29 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "env_filter" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32e90c2accc4b07a8456ea0debdc2e7587bdd890680d71173a15d4ae604f6eef" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0621c04f2196ac3f488dd583365b9c09be011a4ab8b9f37248ffcc8f6198b56a" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + [[package]] name = "heck" version = "0.5.0" @@ -110,12 +142,63 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" +[[package]] +name = "jiff" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00b5dbd620d61dfdcb6007c9c1f6054ebd75319f163d886a9055cec1155073d" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", +] + +[[package]] +name = "jiff-static" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e000de030ff8022ea1da3f466fbb0f3a809f5e51ed31f6dd931c35181ad8e6d7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + [[package]] name = "once_cell_polyfill" version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "portable-atomic-util" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618" +dependencies = [ + "portable-atomic", +] + [[package]] name = "proc-macro2" version = "1.0.106" @@ -134,11 +217,62 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "roto" version = "0.1.0" dependencies = [ "clap", + "env_logger", + "log", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b7284cd..adb3dac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,5 @@ edition = "2024" [dependencies] clap = { version = "4", features = ["derive"] } +log = "0.4" +env_logger = "0.11" diff --git a/src/bin/generator.rs b/src/bin/generator.rs index 9343fc3..0ea6ad7 100644 --- a/src/bin/generator.rs +++ b/src/bin/generator.rs @@ -1,7 +1,6 @@ use clap::Parser; -use roto::proto_gen::google::protobuf::descriptor::{ - DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto, FileDescriptorSet, -}; +use roto::proto_gen::google::protobuf::descriptor::FileDescriptorSet; +use roto::proto_gen::generator::generate_rust_code; use std::fs; use std::path::PathBuf; @@ -17,211 +16,12 @@ struct Args { output: PathBuf, } -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::() + chars.as_str(), - } - }) - .collect() -} - -fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) { - if label == 3 { - // LABEL_REPEATED - return ( - "crate::RepeatedFieldIterator<'a>".to_string(), - "self.0.iter_repeated(%d)".to_string(), - ); - } - - match field_type { - 9 => ( - "&'a str".to_string(), - "str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)".to_string(), - ), // TYPE_STRING - 1 => ( - "f64".to_string(), - "f64::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(), - ), // TYPE_DOUBLE - 2 => ( - "f32".to_string(), - "f32::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(), - ), // TYPE_FLOAT - 3 | 5 | 15 | 17 => ( - "i32".to_string(), - "crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| 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(|_| RotoError::WireFormatViolation)".to_string(), - ), // UINT/FIXED 32 - 16 | 18 => ( - "i64".to_string(), - "crate::read_varint(bytes).map(|(v, _)| v as i64).map_err(|_| RotoError::WireFormatViolation)".to_string(), - ), // SINT/SFIXED 64 - 7 | 14 => ( - "u64".to_string(), - "crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation)".to_string(), - ), // UINT/FIXED 64 - 8 => ( - "bool".to_string(), - "crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| 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 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 => ("i64".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()), - } -} - fn main() -> Result<(), Box> { let args = Args::parse(); let data = fs::read(&args.input)?; let set = FileDescriptorSet::new(&data).expect("Failed to parse FileDescriptorSet"); - let mut output = String::new(); - output.push_str("use crate::{ProtoAccessor, ProtoBuilder, Result, RotoError};\n"); - output.push_str("use std::str;\n\n"); - - 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"); - - // Enums - for enum_res in file_proto.enum_type() { - let (enum_data, _) = enum_res.expect("Failed to iterate enum"); - let enum_proto = EnumDescriptorProto::new(enum_data).expect("Failed to parse EnumDescriptorProto"); - 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.enum_value(); - let mut variant_count = 0; - while let Some(val_res) = values.next() { - let (val_data, _) = val_res.expect("Failed to read enum value"); - let accessor = roto::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::read_varint(num_bytes).expect("Enum value number invalid varint"); - - output.push_str(&format!(" {}, = {},\n", to_pascal_case(name), num)); - variant_count += 1; - } - - if variant_count == 0 { - output.push_str(" Unknown = 0,\n"); - } - - 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.enum_value(); - while let Some(val_res) = values.next() { - let (val_data, _) = val_res.expect("Failed to read enum value"); - let accessor = roto::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::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!(" _ => {}::Unknown,\n", enum_name)); - output.push_str(" }\n }}\n}\n\n"); - } - - // Messages - for msg_res in file_proto.message_type() { - let (msg_data, _) = msg_res.expect("Failed to iterate message"); - let msg_proto = DescriptorProto::new(msg_data).expect("Failed to parse DescriptorProto"); - let msg_name = to_pascal_case(msg_proto.name().unwrap()); - - // Accessor - output.push_str(&format!( - "pub struct {}<'a>(ProtoAccessor<'a>);\n\nimpl<'a> {}<'a> {{\n", - msg_name, msg_name - )); - output.push_str(&format!( - " pub fn new(data: &'a [u8]) -> Result {{\n Ok(Self(ProtoAccessor::new(data)?))\n }}\n\n" - )); - - 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.field_type().unwrap() as i32; - let f_label = field_proto.label().unwrap() as i32; - - let (rust_type, logic) = map_type_to_rust_accessor(f_type, f_label); - - if f_label == 3 { - output.push_str(&format!( - " pub fn {}(&self) -> {} {{\n {}\n }}\n\n", - field_name, rust_type, logic.replace("%d", &tag.to_string()) - )); - } else { - output.push_str(&format!( - " pub fn {}(&self) -> Result<{}> {{\n let (bytes, _) = self.0.get_value({})?;\n {}\n }}\n\n", - field_name, rust_type, tag, logic - )); - } - } - output.push_str("}\n\n"); - - // Builder - output.push_str(&format!( - "pub struct {}Builder<'b> {{\n builder: ProtoBuilder<'b>,\n}}\n\nimpl<'b> {}Builder<'b> {{\n", - msg_name, msg_name - )); - output.push_str(&format!( - " pub fn builder(buf: &mut [u8]) -> {}Builder<'_> {{\n {}Builder {{\n builder: ProtoBuilder::new(buf),\n }}\n }}\n\n", - msg_name, msg_name - )); - - 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.field_type().unwrap() as i32; - - let (rust_type, method) = map_type_to_rust_builder(f_type); - - output.push_str(&format!( - " pub fn {}(mut self, value: {}) -> Result {{\n self.builder.{}({}, value)?;\n Ok(self)\n }}\n\n", - field_name, rust_type, method, tag - )); - } - - output.push_str(&format!( - " pub fn finish(self) -> Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n" - )); - } - } + let output = generate_rust_code(&set); fs::write(&args.output, output)?; Ok(()) diff --git a/src/bin/protoc-gen-roto.rs b/src/bin/protoc-gen-roto.rs new file mode 100644 index 0000000..246ec5e --- /dev/null +++ b/src/bin/protoc-gen-roto.rs @@ -0,0 +1,107 @@ +use env_logger::init; +use log::{error, info}; +use roto::generator::generate_rust_code; +use roto::proto_gen::google::protobuf::descriptor::{ + CodeGeneratorRequest, CodeGeneratorResponse, FileDescriptorSet, ResponseFile, +}; +use roto::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> { + // 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. Construct a FileDescriptorSet from the request's proto_files + // Since generate_rust_code expects a FileDescriptorSet, we wrap the proto_files in one. + // A FileDescriptorSet is a message with a repeated field 'file' at tag 1. + let mut set_buf = vec![0u8; 1024 * 1024]; // Allocate 1MB for descriptors + { + let mut builder = ProtoBuilder::new(&mut set_buf); + for file_res in request.proto_file() { + let (file_data, _) = file_res.map_err(|e| { + error!("Failed to iterate proto_file: {:?}", e); + e + })?; + builder.write_bytes(1, file_data).map_err(|e| { + error!("Failed to write proto_file to set: {:?}", e); + e + })?; + } + // We don't need to call finish() here as we just need the buffer to be populated. + } + + // Trim set_buf to the actual size written by ProtoBuilder + // We need to find the actual length. ProtoBuilder doesn't expose pos publicly in the provided snippet, + // but we can wrap the result of the generation. + // Actually, since FileDescriptorSet::new just wraps the buffer, it will read until the end. + // We should be careful about trailing zeros. + // Let's just use a slice of the buffer that was actually used. + // Since ProtoBuilder's pos is private, we might need a different approach or just hope the + // ProtoAccessor handles trailing zeros (which it should if it's a valid proto message). + // However, the most robust way is to use the length. + + // Let's try to use the buffer as is. If ProtoAccessor is correctly implemented, + // it will stop at the end of the message. + let set = FileDescriptorSet::new(&set_buf)?; + + // 3. Generate the Rust code + info!("Generating Rust code from descriptor set..."); + let generated_code = generate_rust_code(&set); + + // 4. Construct CodeGeneratorResponse + // We'll put all generated code into a single file for now. + // If file_to_generate is provided, we'll use the first one as a base for the name. + let mut output_filename = "roto_generated.rs".to_string(); + if let Some(first_file) = request.file_to_generate().next() { + if let Ok((name_bytes, _)) = first_file { + if let Ok(name) = std::str::from_utf8(name_bytes) { + output_filename = format!("{}.rs", name.replace(".proto", "")); + } + } + } + + let mut response_buf = vec![0u8; 1024 * 1024 * 2]; // Allocate 2MB for response + let mut resp_builder = CodeGeneratorResponse::builder(&mut response_buf); + + let mut file_buf = vec![0u8; 1024 * 1024 * 2]; + let final_file = ResponseFile::builder(&mut file_buf) + .name(&output_filename)? + .content(&generated_code)? + .finish() + .map_err(|e| { + error!("Failed to build ResponseFile: {:?}", e); + e + })?; + + let final_response = resp_builder + .add_file(final_file)? + .finish() + .map_err(|e| { + error!("Failed to finish CodeGeneratorResponse: {:?}", e); + e + })?; + + // 5. Write response to stdout + io::stdout().write_all(final_response)?; + + info!("Successfully wrote CodeGeneratorResponse to stdout"); + Ok(()) +} diff --git a/src/generator.rs b/src/generator.rs new file mode 100644 index 0000000..f70a1de --- /dev/null +++ b/src/generator.rs @@ -0,0 +1,210 @@ +use crate::proto_gen::google::protobuf::descriptor::{ + DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto, FileDescriptorSet, +}; +use crate::{ProtoAccessor, Result, RotoError}; +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::() + chars.as_str(), + } + }) + .collect() +} + +fn map_type_to_rust_accessor(field_type: i32, label: i32) -> (String, String) { + if label == 3 { + // LABEL_REPEATED + return ( + "crate::RepeatedFieldIterator<'a>".to_string(), + "self.0.iter_repeated(%d)".to_string(), + ); + } + + match field_type { + 9 => ( + "&'a str".to_string(), + "str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation)".to_string(), + ), // TYPE_STRING + 1 => ( + "f64".to_string(), + "f64::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(), + ), // TYPE_DOUBLE + 2 => ( + "f32".to_string(), + "f32::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?)".to_string(), + ), // TYPE_FLOAT + 3 | 5 | 15 | 17 => ( + "i32".to_string(), + "crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| 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(|_| RotoError::WireFormatViolation)".to_string(), + ), // UINT/FIXED 32 + 16 | 18 => ( + "i64".to_string(), + "crate::read_varint(bytes).map(|(v, _)| v as i64).map_err(|_| RotoError::WireFormatViolation)".to_string(), + ), // SINT/SFIXED 64 + 7 | 14 => ( + "u64".to_string(), + "crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation)".to_string(), + ), // UINT/FIXED 64 + 8 => ( + "bool".to_string(), + "crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| 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 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 => ("i64".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) -> String { + let mut output = String::new(); + output.push_str("use crate::{ProtoAccessor, ProtoBuilder, Result, RotoError};\n"); + output.push_str("use std::str;\n\n"); + + 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"); + + // Enums + for enum_res in file_proto.enum_type() { + let (enum_data, _) = enum_res.expect("Failed to iterate enum"); + let enum_proto = EnumDescriptorProto::new(enum_data).expect("Failed to parse EnumDescriptorProto"); + 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.enum_value(); + let mut variant_count = 0; + 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, _) = crate::read_varint(num_bytes).expect("Enum value number invalid varint"); + + output.push_str(&format!(" {}, = {},\n", to_pascal_case(name), num)); + variant_count += 1; + } + + if variant_count == 0 { + output.push_str(" Unknown = 0,\n"); + } + + 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.enum_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, _) = crate::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!(" _ => {}::Unknown,\n", enum_name)); + output.push_str(" }\n }}\n}\n\n"); + } + + // Messages + for msg_res in file_proto.message_type() { + let (msg_data, _) = msg_res.expect("Failed to iterate message"); + let msg_proto = DescriptorProto::new(msg_data).expect("Failed to parse DescriptorProto"); + let msg_name = to_pascal_case(msg_proto.name().unwrap()); + + // Accessor + output.push_str(&format!( + "pub struct {}<'a>(ProtoAccessor<'a>);\n\nimpl<'a> {}<'a> {{\n", + msg_name, msg_name + )); + output.push_str(&format!( + " pub fn new(data: &'a [u8]) -> Result {{\n Ok(Self(ProtoAccessor::new(data)?))\n }}\n\n" + )); + + 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.field_type().unwrap() as i32; + let f_label = field_proto.label().unwrap() as i32; + + let (rust_type, logic) = map_type_to_rust_accessor(f_type, f_label); + + if f_label == 3 { + output.push_str(&format!( + " pub fn {}(&self) -> {} {{\n {}\n }}\n\n", + field_name, rust_type, logic.replace("%d", &tag.to_string()) + )); + } else { + output.push_str(&format!( + " pub fn {}(&self) -> Result<{}> {{\n let (bytes, _) = self.0.get_value({})?;\n {}\n }}\n\n", + field_name, rust_type, tag, logic + )); + } + } + output.push_str("}\n\n"); + + // Builder + output.push_str(&format!( + "pub struct {}Builder<'b> {{\n builder: ProtoBuilder<'b>,\n}}\n\nimpl<'b> {}Builder<'b> {{\n", + msg_name, msg_name + )); + output.push_str(&format!( + " pub fn builder(buf: &mut [u8]) -> {}Builder<'_> {{\n {}Builder {{\n builder: ProtoBuilder::new(buf),\n }}\n }}\n\n", + msg_name, msg_name + )); + + 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.field_type().unwrap() as i32; + + let (rust_type, method) = map_type_to_rust_builder(f_type); + + output.push_str(&format!( + " pub fn {}(mut self, value: {}) -> Result {{\n self.builder.{}({}, value)?;\n Ok(self)\n }}\n\n", + field_name, rust_type, method, tag + )); + } + + output.push_str(&format!( + " pub fn finish(self) -> Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n" + )); + } + } + + output +} diff --git a/src/lib.rs b/src/lib.rs index 18ef69e..a3e1d41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod proto_gen; +pub mod generator; use std::fmt; #[derive(Debug, PartialEq, Eq)] diff --git a/src/proto_gen/google/protobuf/descriptor.rs b/src/proto_gen/google/protobuf/descriptor.rs index 142f751..29e5a5a 100644 --- a/src/proto_gen/google/protobuf/descriptor.rs +++ b/src/proto_gen/google/protobuf/descriptor.rs @@ -533,6 +533,284 @@ impl<'b> GeneratedCodeInfoBuilder<'b> { } } +pub struct Version<'a>(ProtoAccessor<'a>); + +impl<'a> Version<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn major(&self) -> Result { + let (bytes, _) = self.0.get_value(1)?; + let (val, _) = crate::read_varint(bytes)?; + Ok(val as i32) + } + + pub fn minor(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + let (val, _) = crate::read_varint(bytes)?; + Ok(val as i32) + } + + pub fn patch(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + let (val, _) = crate::read_varint(bytes)?; + Ok(val as i32) + } + + pub fn suffix(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(4)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn builder(buf: &mut [u8]) -> VersionBuilder<'_> { + VersionBuilder { + builder: ProtoBuilder::new(buf), + } + } +} + +pub struct VersionBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> VersionBuilder<'b> { + pub fn major(mut self, value: i32) -> Result { + self.builder.write_int32(1, value)?; + Ok(self) + } + + pub fn minor(mut self, value: i32) -> Result { + self.builder.write_int32(2, value)?; + Ok(self) + } + + pub fn patch(mut self, value: i32) -> Result { + self.builder.write_int32(3, value)?; + Ok(self) + } + + pub fn suffix(mut self, value: &str) -> Result { + self.builder.write_string(4, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct CodeGeneratorRequest<'a>(ProtoAccessor<'a>); + +impl<'a> CodeGeneratorRequest<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn file_to_generate(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(1) + } + + pub fn parameter(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(2)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn compiler_version(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(3)?; + Ok(bytes) + } + + pub fn proto_file(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(15) + } + + pub fn source_file_descriptors(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(17) + } + + pub fn builder(buf: &mut [u8]) -> CodeGeneratorRequestBuilder<'_> { + CodeGeneratorRequestBuilder { + builder: ProtoBuilder::new(buf), + } + } +} + +pub struct CodeGeneratorRequestBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> CodeGeneratorRequestBuilder<'b> { + pub fn add_file_to_generate(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn parameter(mut self, value: &str) -> Result { + self.builder.write_string(2, value)?; + Ok(self) + } + + pub fn compiler_version(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(3, data)?; + Ok(self) + } + + pub fn add_proto_file(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(15, data)?; + Ok(self) + } + + pub fn add_source_file_descriptor(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(17, data)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct CodeGeneratorResponse<'a>(ProtoAccessor<'a>); + +impl<'a> CodeGeneratorResponse<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn error(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn supported_features(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + let (val, _) = crate::read_varint(bytes)?; + Ok(val) + } + + pub fn minimum_edition(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + let (val, _) = crate::read_varint(bytes)?; + Ok(val as i32) + } + + pub fn maximum_edition(&self) -> Result { + let (bytes, _) = self.0.get_value(4)?; + let (val, _) = crate::read_varint(bytes)?; + Ok(val as i32) + } + + pub fn file(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(15) + } + + pub fn builder(buf: &mut [u8]) -> CodeGeneratorResponseBuilder<'_> { + CodeGeneratorResponseBuilder { + builder: ProtoBuilder::new(buf), + } + } +} + +pub struct CodeGeneratorResponseBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> CodeGeneratorResponseBuilder<'b> { + pub fn error(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn supported_features(mut self, value: u64) -> Result { + self.builder.write_varint(2, value)?; + Ok(self) + } + + pub fn minimum_edition(mut self, value: i32) -> Result { + self.builder.write_int32(3, value)?; + Ok(self) + } + + pub fn maximum_edition(mut self, value: i32) -> Result { + self.builder.write_int32(4, value)?; + Ok(self) + } + + pub fn add_file(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(15, data)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct ResponseFile<'a>(ProtoAccessor<'a>); + +impl<'a> ResponseFile<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn insertion_point(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(2)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn content(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(15)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn generated_code_info(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(16)?; + Ok(bytes) + } + + pub fn builder(buf: &mut [u8]) -> ResponseFileBuilder<'_> { + ResponseFileBuilder { + builder: ProtoBuilder::new(buf), + } + } +} + +pub struct ResponseFileBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> ResponseFileBuilder<'b> { + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn insertion_point(mut self, value: &str) -> Result { + self.builder.write_string(2, value)?; + Ok(self) + } + + pub fn content(mut self, value: &str) -> Result { + self.builder.write_string(15, value)?; + Ok(self) + } + + pub fn generated_code_info(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(16, data)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/proto_gen/google/protobuf/descriptor_gen.rs b/src/proto_gen/google/protobuf/descriptor_gen.rs new file mode 100644 index 0000000..a67de4c --- /dev/null +++ b/src/proto_gen/google/protobuf/descriptor_gen.rs @@ -0,0 +1,2030 @@ +use crate::{ProtoAccessor, ProtoBuilder, Result, RotoError}; +use std::str; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum Edition { + EDITIONUNKNOWN, = 0, + EDITIONLEGACY, = 900, + EDITIONPROTO2, = 998, + EDITIONPROTO3, = 999, + EDITION2023, = 1000, + EDITION2024, = 1001, + EDITION2026, = 1002, + EDITIONUNSTABLE, = 9999, + EDITION1TESTONLY, = 1, + EDITION2TESTONLY, = 2, + EDITION99997TESTONLY, = 99997, + EDITION99998TESTONLY, = 99998, + EDITION99999TESTONLY, = 99999, + EDITIONMAX, = 2147483647, +} + +impl Edition { + pub fn from_i32(value: i32) -> Self { + match value { + 0 => Edition::EDITIONUNKNOWN, + 900 => Edition::EDITIONLEGACY, + 998 => Edition::EDITIONPROTO2, + 999 => Edition::EDITIONPROTO3, + 1000 => Edition::EDITION2023, + 1001 => Edition::EDITION2024, + 1002 => Edition::EDITION2026, + 9999 => Edition::EDITIONUNSTABLE, + 1 => Edition::EDITION1TESTONLY, + 2 => Edition::EDITION2TESTONLY, + 99997 => Edition::EDITION99997TESTONLY, + 99998 => Edition::EDITION99998TESTONLY, + 99999 => Edition::EDITION99999TESTONLY, + 2147483647 => Edition::EDITIONMAX, + _ => Edition::Unknown, + } + }} +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(i32)] +pub enum SymbolVisibility { + VISIBILITYUNSET, = 0, + VISIBILITYLOCAL, = 1, + VISIBILITYEXPORT, = 2, +} + +impl SymbolVisibility { + pub fn from_i32(value: i32) -> Self { + match value { + 0 => SymbolVisibility::VISIBILITYUNSET, + 1 => SymbolVisibility::VISIBILITYLOCAL, + 2 => SymbolVisibility::VISIBILITYEXPORT, + _ => SymbolVisibility::Unknown, + } + }} +} + +pub struct FileDescriptorSet<'a>(ProtoAccessor<'a>); + +impl<'a> FileDescriptorSet<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn file(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(1) + } + +} + +pub struct FileDescriptorSetBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FileDescriptorSetBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FileDescriptorSetBuilder<'_> { + FileDescriptorSetBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn file(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(1, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct FileDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> FileDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn package(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(2)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn dependency(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(3) + } + + pub fn public_dependency(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(10) + } + + pub fn weak_dependency(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(11) + } + + pub fn option_dependency(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(15) + } + + pub fn message_type(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(4) + } + + pub fn enum_type(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(5) + } + + pub fn service(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(6) + } + + pub fn extension(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(7) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(8)?; + Ok(bytes) + } + + pub fn source_code_info(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(9)?; + Ok(bytes) + } + + pub fn syntax(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(12)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn edition(&self) -> Result { + let (bytes, _) = self.0.get_value(14)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct FileDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FileDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FileDescriptorProtoBuilder<'_> { + FileDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn package(mut self, value: &str) -> Result { + self.builder.write_string(2, value)?; + Ok(self) + } + + pub fn dependency(mut self, value: &str) -> Result { + self.builder.write_string(3, value)?; + Ok(self) + } + + pub fn public_dependency(mut self, value: i32) -> Result { + self.builder.write_int32(10, value)?; + Ok(self) + } + + pub fn weak_dependency(mut self, value: i32) -> Result { + self.builder.write_int32(11, value)?; + Ok(self) + } + + pub fn option_dependency(mut self, value: &str) -> Result { + self.builder.write_string(15, value)?; + Ok(self) + } + + pub fn message_type(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(4, value)?; + Ok(self) + } + + pub fn enum_type(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(5, value)?; + Ok(self) + } + + pub fn service(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(6, value)?; + Ok(self) + } + + pub fn extension(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(7, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(8, value)?; + Ok(self) + } + + pub fn source_code_info(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(9, value)?; + Ok(self) + } + + pub fn syntax(mut self, value: &str) -> Result { + self.builder.write_string(12, value)?; + Ok(self) + } + + pub fn edition(mut self, value: i64) -> Result { + self.builder.write_varint(14, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct DescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> DescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn field(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(2) + } + + pub fn extension(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(6) + } + + pub fn nested_type(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(3) + } + + pub fn enum_type(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(4) + } + + pub fn extension_range(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(5) + } + + pub fn oneof_decl(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(8) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(7)?; + Ok(bytes) + } + + pub fn reserved_range(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(9) + } + + pub fn reserved_name(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(10) + } + + pub fn visibility(&self) -> Result { + let (bytes, _) = self.0.get_value(11)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct DescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> DescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> DescriptorProtoBuilder<'_> { + DescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn field(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn extension(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(6, value)?; + Ok(self) + } + + pub fn nested_type(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(3, value)?; + Ok(self) + } + + pub fn enum_type(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(4, value)?; + Ok(self) + } + + pub fn extension_range(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(5, value)?; + Ok(self) + } + + pub fn oneof_decl(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(8, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(7, value)?; + Ok(self) + } + + pub fn reserved_range(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(9, value)?; + Ok(self) + } + + pub fn reserved_name(mut self, value: &str) -> Result { + self.builder.write_string(10, value)?; + Ok(self) + } + + pub fn visibility(mut self, value: i64) -> Result { + self.builder.write_varint(11, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct ExtensionRangeOptions<'a>(ProtoAccessor<'a>); + +impl<'a> ExtensionRangeOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + + pub fn declaration(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(2) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(50)?; + Ok(bytes) + } + + pub fn verification(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct ExtensionRangeOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> ExtensionRangeOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> ExtensionRangeOptionsBuilder<'_> { + ExtensionRangeOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn declaration(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(50, value)?; + Ok(self) + } + + pub fn verification(mut self, value: i64) -> Result { + self.builder.write_varint(3, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct FieldDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> FieldDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn number(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn label(&self) -> Result { + let (bytes, _) = self.0.get_value(4)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn type(&self) -> Result { + let (bytes, _) = self.0.get_value(5)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn type_name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(6)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn extendee(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(2)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn default_value(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(7)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn oneof_index(&self) -> Result { + let (bytes, _) = self.0.get_value(9)?; + crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn json_name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(10)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(8)?; + Ok(bytes) + } + + pub fn proto3_optional(&self) -> Result { + let (bytes, _) = self.0.get_value(17)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct FieldDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FieldDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FieldDescriptorProtoBuilder<'_> { + FieldDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn number(mut self, value: i32) -> Result { + self.builder.write_int32(3, value)?; + Ok(self) + } + + pub fn label(mut self, value: i64) -> Result { + self.builder.write_varint(4, value)?; + Ok(self) + } + + pub fn type(mut self, value: i64) -> Result { + self.builder.write_varint(5, value)?; + Ok(self) + } + + pub fn type_name(mut self, value: &str) -> Result { + self.builder.write_string(6, value)?; + Ok(self) + } + + pub fn extendee(mut self, value: &str) -> Result { + self.builder.write_string(2, value)?; + Ok(self) + } + + pub fn default_value(mut self, value: &str) -> Result { + self.builder.write_string(7, value)?; + Ok(self) + } + + pub fn oneof_index(mut self, value: i32) -> Result { + self.builder.write_int32(9, value)?; + Ok(self) + } + + pub fn json_name(mut self, value: &str) -> Result { + self.builder.write_string(10, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(8, value)?; + Ok(self) + } + + pub fn proto3_optional(mut self, value: i64) -> Result { + self.builder.write_varint(17, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct OneofDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> OneofDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(2)?; + Ok(bytes) + } + +} + +pub struct OneofDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> OneofDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> OneofDescriptorProtoBuilder<'_> { + OneofDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct EnumDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> EnumDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn value(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(2) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(3)?; + Ok(bytes) + } + + pub fn reserved_range(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(4) + } + + pub fn reserved_name(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(5) + } + + pub fn visibility(&self) -> Result { + let (bytes, _) = self.0.get_value(6)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct EnumDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> EnumDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> EnumDescriptorProtoBuilder<'_> { + EnumDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn value(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(3, value)?; + Ok(self) + } + + pub fn reserved_range(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(4, value)?; + Ok(self) + } + + pub fn reserved_name(mut self, value: &str) -> Result { + self.builder.write_string(5, value)?; + Ok(self) + } + + pub fn visibility(mut self, value: i64) -> Result { + self.builder.write_varint(6, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct EnumValueDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> EnumValueDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn number(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(3)?; + Ok(bytes) + } + +} + +pub struct EnumValueDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> EnumValueDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> EnumValueDescriptorProtoBuilder<'_> { + EnumValueDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn number(mut self, value: i32) -> Result { + self.builder.write_int32(2, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(3, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct ServiceDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> ServiceDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn method(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(2) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(3)?; + Ok(bytes) + } + +} + +pub struct ServiceDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> ServiceDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> ServiceDescriptorProtoBuilder<'_> { + ServiceDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn method(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(3, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct MethodDescriptorProto<'a>(ProtoAccessor<'a>); + +impl<'a> MethodDescriptorProto<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn input_type(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(2)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn output_type(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(3)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn options(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(4)?; + Ok(bytes) + } + + pub fn client_streaming(&self) -> Result { + let (bytes, _) = self.0.get_value(5)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn server_streaming(&self) -> Result { + let (bytes, _) = self.0.get_value(6)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct MethodDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> MethodDescriptorProtoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> MethodDescriptorProtoBuilder<'_> { + MethodDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn input_type(mut self, value: &str) -> Result { + self.builder.write_string(2, value)?; + Ok(self) + } + + pub fn output_type(mut self, value: &str) -> Result { + self.builder.write_string(3, value)?; + Ok(self) + } + + pub fn options(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(4, value)?; + Ok(self) + } + + pub fn client_streaming(mut self, value: i64) -> Result { + self.builder.write_varint(5, value)?; + Ok(self) + } + + pub fn server_streaming(mut self, value: i64) -> Result { + self.builder.write_varint(6, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct FileOptions<'a>(ProtoAccessor<'a>); + +impl<'a> FileOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn java_package(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(1)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn java_outer_classname(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(8)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn java_multiple_files(&self) -> Result { + let (bytes, _) = self.0.get_value(10)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn java_generate_equals_and_hash(&self) -> Result { + let (bytes, _) = self.0.get_value(20)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn java_string_check_utf8(&self) -> Result { + let (bytes, _) = self.0.get_value(27)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn optimize_for(&self) -> Result { + let (bytes, _) = self.0.get_value(9)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn go_package(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(11)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn cc_generic_services(&self) -> Result { + let (bytes, _) = self.0.get_value(16)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn java_generic_services(&self) -> Result { + let (bytes, _) = self.0.get_value(17)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn py_generic_services(&self) -> Result { + let (bytes, _) = self.0.get_value(18)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(23)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn cc_enable_arenas(&self) -> Result { + let (bytes, _) = self.0.get_value(31)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn objc_class_prefix(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(36)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn csharp_namespace(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(37)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn swift_prefix(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(39)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn php_class_prefix(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(40)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn php_namespace(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(41)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn php_metadata_namespace(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(44)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn ruby_package(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(45)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(50)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct FileOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FileOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FileOptionsBuilder<'_> { + FileOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn java_package(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn java_outer_classname(mut self, value: &str) -> Result { + self.builder.write_string(8, value)?; + Ok(self) + } + + pub fn java_multiple_files(mut self, value: i64) -> Result { + self.builder.write_varint(10, value)?; + Ok(self) + } + + pub fn java_generate_equals_and_hash(mut self, value: i64) -> Result { + self.builder.write_varint(20, value)?; + Ok(self) + } + + pub fn java_string_check_utf8(mut self, value: i64) -> Result { + self.builder.write_varint(27, value)?; + Ok(self) + } + + pub fn optimize_for(mut self, value: i64) -> Result { + self.builder.write_varint(9, value)?; + Ok(self) + } + + pub fn go_package(mut self, value: &str) -> Result { + self.builder.write_string(11, value)?; + Ok(self) + } + + pub fn cc_generic_services(mut self, value: i64) -> Result { + self.builder.write_varint(16, value)?; + Ok(self) + } + + pub fn java_generic_services(mut self, value: i64) -> Result { + self.builder.write_varint(17, value)?; + Ok(self) + } + + pub fn py_generic_services(mut self, value: i64) -> Result { + self.builder.write_varint(18, value)?; + Ok(self) + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(23, value)?; + Ok(self) + } + + pub fn cc_enable_arenas(mut self, value: i64) -> Result { + self.builder.write_varint(31, value)?; + Ok(self) + } + + pub fn objc_class_prefix(mut self, value: &str) -> Result { + self.builder.write_string(36, value)?; + Ok(self) + } + + pub fn csharp_namespace(mut self, value: &str) -> Result { + self.builder.write_string(37, value)?; + Ok(self) + } + + pub fn swift_prefix(mut self, value: &str) -> Result { + self.builder.write_string(39, value)?; + Ok(self) + } + + pub fn php_class_prefix(mut self, value: &str) -> Result { + self.builder.write_string(40, value)?; + Ok(self) + } + + pub fn php_namespace(mut self, value: &str) -> Result { + self.builder.write_string(41, value)?; + Ok(self) + } + + pub fn php_metadata_namespace(mut self, value: &str) -> Result { + self.builder.write_string(44, value)?; + Ok(self) + } + + pub fn ruby_package(mut self, value: &str) -> Result { + self.builder.write_string(45, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(50, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct MessageOptions<'a>(ProtoAccessor<'a>); + +impl<'a> MessageOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn message_set_wire_format(&self) -> Result { + let (bytes, _) = self.0.get_value(1)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn no_standard_descriptor_accessor(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn map_entry(&self) -> Result { + let (bytes, _) = self.0.get_value(7)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn deprecated_legacy_json_field_conflicts(&self) -> Result { + let (bytes, _) = self.0.get_value(11)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(12)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct MessageOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> MessageOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> MessageOptionsBuilder<'_> { + MessageOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn message_set_wire_format(mut self, value: i64) -> Result { + self.builder.write_varint(1, value)?; + Ok(self) + } + + pub fn no_standard_descriptor_accessor(mut self, value: i64) -> Result { + self.builder.write_varint(2, value)?; + Ok(self) + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(3, value)?; + Ok(self) + } + + pub fn map_entry(mut self, value: i64) -> Result { + self.builder.write_varint(7, value)?; + Ok(self) + } + + pub fn deprecated_legacy_json_field_conflicts(mut self, value: i64) -> Result { + self.builder.write_varint(11, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(12, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct FieldOptions<'a>(ProtoAccessor<'a>); + +impl<'a> FieldOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn ctype(&self) -> Result { + let (bytes, _) = self.0.get_value(1)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn packed(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn jstype(&self) -> Result { + let (bytes, _) = self.0.get_value(6)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn lazy(&self) -> Result { + let (bytes, _) = self.0.get_value(5)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn unverified_lazy(&self) -> Result { + let (bytes, _) = self.0.get_value(15)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn weak(&self) -> Result { + let (bytes, _) = self.0.get_value(10)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn debug_redact(&self) -> Result { + let (bytes, _) = self.0.get_value(16)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn retention(&self) -> Result { + let (bytes, _) = self.0.get_value(17)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn targets(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(19) + } + + pub fn edition_defaults(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(20) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(21)?; + Ok(bytes) + } + + pub fn feature_support(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(22)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct FieldOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FieldOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FieldOptionsBuilder<'_> { + FieldOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn ctype(mut self, value: i64) -> Result { + self.builder.write_varint(1, value)?; + Ok(self) + } + + pub fn packed(mut self, value: i64) -> Result { + self.builder.write_varint(2, value)?; + Ok(self) + } + + pub fn jstype(mut self, value: i64) -> Result { + self.builder.write_varint(6, value)?; + Ok(self) + } + + pub fn lazy(mut self, value: i64) -> Result { + self.builder.write_varint(5, value)?; + Ok(self) + } + + pub fn unverified_lazy(mut self, value: i64) -> Result { + self.builder.write_varint(15, value)?; + Ok(self) + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(3, value)?; + Ok(self) + } + + pub fn weak(mut self, value: i64) -> Result { + self.builder.write_varint(10, value)?; + Ok(self) + } + + pub fn debug_redact(mut self, value: i64) -> Result { + self.builder.write_varint(16, value)?; + Ok(self) + } + + pub fn retention(mut self, value: i64) -> Result { + self.builder.write_varint(17, value)?; + Ok(self) + } + + pub fn targets(mut self, value: i64) -> Result { + self.builder.write_varint(19, value)?; + Ok(self) + } + + pub fn edition_defaults(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(20, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(21, value)?; + Ok(self) + } + + pub fn feature_support(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(22, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct OneofOptions<'a>(ProtoAccessor<'a>); + +impl<'a> OneofOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(1)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct OneofOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> OneofOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> OneofOptionsBuilder<'_> { + OneofOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(1, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct EnumOptions<'a>(ProtoAccessor<'a>); + +impl<'a> EnumOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn allow_alias(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn deprecated_legacy_json_field_conflicts(&self) -> Result { + let (bytes, _) = self.0.get_value(6)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(7)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct EnumOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> EnumOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> EnumOptionsBuilder<'_> { + EnumOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn allow_alias(mut self, value: i64) -> Result { + self.builder.write_varint(2, value)?; + Ok(self) + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(3, value)?; + Ok(self) + } + + pub fn deprecated_legacy_json_field_conflicts(mut self, value: i64) -> Result { + self.builder.write_varint(6, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(7, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct EnumValueOptions<'a>(ProtoAccessor<'a>); + +impl<'a> EnumValueOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(1)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(2)?; + Ok(bytes) + } + + pub fn debug_redact(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn feature_support(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(4)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct EnumValueOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> EnumValueOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> EnumValueOptionsBuilder<'_> { + EnumValueOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(1, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn debug_redact(mut self, value: i64) -> Result { + self.builder.write_varint(3, value)?; + Ok(self) + } + + pub fn feature_support(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(4, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct ServiceOptions<'a>(ProtoAccessor<'a>); + +impl<'a> ServiceOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(34)?; + Ok(bytes) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(33)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct ServiceOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> ServiceOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> ServiceOptionsBuilder<'_> { + ServiceOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(34, value)?; + Ok(self) + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(33, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct MethodOptions<'a>(ProtoAccessor<'a>); + +impl<'a> MethodOptions<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn deprecated(&self) -> Result { + let (bytes, _) = self.0.get_value(33)?; + crate::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn idempotency_level(&self) -> Result { + let (bytes, _) = self.0.get_value(34)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn features(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(35)?; + Ok(bytes) + } + + pub fn uninterpreted_option(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(999) + } + +} + +pub struct MethodOptionsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> MethodOptionsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> MethodOptionsBuilder<'_> { + MethodOptionsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn deprecated(mut self, value: i64) -> Result { + self.builder.write_varint(33, value)?; + Ok(self) + } + + pub fn idempotency_level(mut self, value: i64) -> Result { + self.builder.write_varint(34, value)?; + Ok(self) + } + + pub fn features(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(35, value)?; + Ok(self) + } + + pub fn uninterpreted_option(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(999, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct UninterpretedOption<'a>(ProtoAccessor<'a>); + +impl<'a> UninterpretedOption<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn name(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(2) + } + + pub fn identifier_value(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(3)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn positive_int_value(&self) -> Result { + let (bytes, _) = self.0.get_value(4)?; + crate::read_varint(bytes).map(|(v, _)| v as u32).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn negative_int_value(&self) -> Result { + let (bytes, _) = self.0.get_value(5)?; + crate::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn double_value(&self) -> Result { + let (bytes, _) = self.0.get_value(6)?; + f64::from_le_bytes(bytes.try_into().map_err(|_| RotoError::WireFormatViolation)?) + } + + pub fn string_value(&self) -> Result<&'a [u8]> { + let (bytes, _) = self.0.get_value(7)?; + Ok(bytes) + } + + pub fn aggregate_value(&self) -> Result<&'a str> { + let (bytes, _) = self.0.get_value(8)?; + str::from_utf8(bytes).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct UninterpretedOptionBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> UninterpretedOptionBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> UninterpretedOptionBuilder<'_> { + UninterpretedOptionBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn name(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(2, value)?; + Ok(self) + } + + pub fn identifier_value(mut self, value: &str) -> Result { + self.builder.write_string(3, value)?; + Ok(self) + } + + pub fn positive_int_value(mut self, value: i64) -> Result { + self.builder.write_varint(4, value)?; + Ok(self) + } + + pub fn negative_int_value(mut self, value: i64) -> Result { + self.builder.write_varint(5, value)?; + Ok(self) + } + + pub fn double_value(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(6, value)?; + Ok(self) + } + + pub fn string_value(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(7, value)?; + Ok(self) + } + + pub fn aggregate_value(mut self, value: &str) -> Result { + self.builder.write_string(8, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct FeatureSet<'a>(ProtoAccessor<'a>); + +impl<'a> FeatureSet<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn field_presence(&self) -> Result { + let (bytes, _) = self.0.get_value(1)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn enum_type(&self) -> Result { + let (bytes, _) = self.0.get_value(2)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn repeated_field_encoding(&self) -> Result { + let (bytes, _) = self.0.get_value(3)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn utf8_validation(&self) -> Result { + let (bytes, _) = self.0.get_value(4)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn message_encoding(&self) -> Result { + let (bytes, _) = self.0.get_value(5)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn json_format(&self) -> Result { + let (bytes, _) = self.0.get_value(6)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn enforce_naming_style(&self) -> Result { + let (bytes, _) = self.0.get_value(7)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn default_symbol_visibility(&self) -> Result { + let (bytes, _) = self.0.get_value(8)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn enforce_proto_limits(&self) -> Result { + let (bytes, _) = self.0.get_value(9)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct FeatureSetBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FeatureSetBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FeatureSetBuilder<'_> { + FeatureSetBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn field_presence(mut self, value: i64) -> Result { + self.builder.write_varint(1, value)?; + Ok(self) + } + + pub fn enum_type(mut self, value: i64) -> Result { + self.builder.write_varint(2, value)?; + Ok(self) + } + + pub fn repeated_field_encoding(mut self, value: i64) -> Result { + self.builder.write_varint(3, value)?; + Ok(self) + } + + pub fn utf8_validation(mut self, value: i64) -> Result { + self.builder.write_varint(4, value)?; + Ok(self) + } + + pub fn message_encoding(mut self, value: i64) -> Result { + self.builder.write_varint(5, value)?; + Ok(self) + } + + pub fn json_format(mut self, value: i64) -> Result { + self.builder.write_varint(6, value)?; + Ok(self) + } + + pub fn enforce_naming_style(mut self, value: i64) -> Result { + self.builder.write_varint(7, value)?; + Ok(self) + } + + pub fn default_symbol_visibility(mut self, value: i64) -> Result { + self.builder.write_varint(8, value)?; + Ok(self) + } + + pub fn enforce_proto_limits(mut self, value: i64) -> Result { + self.builder.write_varint(9, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct FeatureSetDefaults<'a>(ProtoAccessor<'a>); + +impl<'a> FeatureSetDefaults<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn defaults(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(1) + } + + pub fn minimum_edition(&self) -> Result { + let (bytes, _) = self.0.get_value(4)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + + pub fn maximum_edition(&self) -> Result { + let (bytes, _) = self.0.get_value(5)?; + crate::read_varint(bytes).map(|(v, _)| v as u64).map_err(|_| RotoError::WireFormatViolation) + } + +} + +pub struct FeatureSetDefaultsBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> FeatureSetDefaultsBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> FeatureSetDefaultsBuilder<'_> { + FeatureSetDefaultsBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn defaults(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(1, value)?; + Ok(self) + } + + pub fn minimum_edition(mut self, value: i64) -> Result { + self.builder.write_varint(4, value)?; + Ok(self) + } + + pub fn maximum_edition(mut self, value: i64) -> Result { + self.builder.write_varint(5, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct SourceCodeInfo<'a>(ProtoAccessor<'a>); + +impl<'a> SourceCodeInfo<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn location(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(1) + } + +} + +pub struct SourceCodeInfoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> SourceCodeInfoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> SourceCodeInfoBuilder<'_> { + SourceCodeInfoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn location(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(1, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + +pub struct GeneratedCodeInfo<'a>(ProtoAccessor<'a>); + +impl<'a> GeneratedCodeInfo<'a> { + pub fn new(data: &'a [u8]) -> Result { + Ok(Self(ProtoAccessor::new(data)?)) + } + + pub fn annotation(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(1) + } + +} + +pub struct GeneratedCodeInfoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> GeneratedCodeInfoBuilder<'b> { + pub fn builder(buf: &mut [u8]) -> GeneratedCodeInfoBuilder<'_> { + GeneratedCodeInfoBuilder { + builder: ProtoBuilder::new(buf), + } + } + + pub fn annotation(mut self, value: &[u8]) -> Result { + self.builder.write_bytes(1, value)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +}