From 33449d00c8bbf796bbb7e6810a40e8406733c94e Mon Sep 17 00:00:00 2001 From: charles Date: Sat, 2 May 2026 22:12:45 -0700 Subject: [PATCH] Add EnumDescriptorProto and clap dependency --- Cargo.lock | 179 +++++++++++++++ Cargo.toml | 1 + data/descriptor.desc | Bin 0 -> 13915 bytes src/bin/generator.rs | 228 ++++++++++++++++++++ src/proto_gen/google/protobuf/descriptor.rs | 61 ++++++ 5 files changed, 469 insertions(+) create mode 100644 data/descriptor.desc create mode 100644 src/bin/generator.rs diff --git a/Cargo.lock b/Cargo.lock index 30421de..59c303c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,185 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + [[package]] name = "roto" version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml index 3a4d09e..b7284cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4", features = ["derive"] } diff --git a/data/descriptor.desc b/data/descriptor.desc new file mode 100644 index 0000000000000000000000000000000000000000..8378d0bce7d6117f241e32695bde325bc0859406 GIT binary patch literal 13915 zcm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Ef_`Poon7FSc?`x?Kv{jBe=yuJp zq_kqMGCU<~Ymguw3y_$qGGbB7c)98prKs z@UBUz8INs5Yhw1t?lCEmzTM_VW$IXFRtE)Qr%Ks&7`wC@E1a*BYCp8tnz%6sY2Rv!yTING3J; zGH2t6@sP-#jBUoFI>Wo+^{@iG1PB5>#U89XDPNe7G>3BiH`yuS^?dh_GF6nft&`8yPbo)=hSL zu4_lHpUo1e~TDji%3yT59n4-9x-BSDmtlx6~AY;wf0z_oi6L z(0fc*?obja78}37ueqE!O;k*1?9f`GSInVZ4ar)*FuC!CYP5f_q<1G zm&b{g_z+G=7D3|i!2OA)$M7?zF9Z)a^M8p>?eueX+`lJ#1NDwm?)Le}ZcmdGY6{=< zDK~zmv7`rv!k`I>uFf z*%bxfRABlOi}A?DyW=n}C4VVX_Xd3AtL$-)ZwFVfcofhHCZLDyDf6sCsHky|Hu4uF z{}^Md;%=PsVgGIaHe+_bKfohaNFP7>L&x zqWgv?H4;RR6sS$V=NxqVLW_7xEk58e#8Yb0mU>EUvZU)A+Nc}bqBWfnkbR~DaI1sv4#saD%F`j^cwCg6i|9n{ksW- zGao3TW>Non6!;2W*y1tbvPX`X`U*VW4Cqz;<5|#U{gVU&qV5sy)|;&oz2f}+Iyrq0 z{3Q!dBwJRaUb~Gog$9bDffxe+S&!m%{Ch1l_(#9DG&_7HH2lZW;nzdMe}dsPv}Bcb zwoAA4U*ccl!Y9O*+@LXkZIO`Y6dT=E! z(j@n~_V&x_ue1&ZJ^bNBVK6!f{tk?8SKUj&o%?X_7buQn3)gnae-_)gCjM%yAUNS~ O!V0Fd-*eh@vi=VSi2cL> literal 0 HcmV?d00001 diff --git a/src/bin/generator.rs b/src/bin/generator.rs new file mode 100644 index 0000000..9343fc3 --- /dev/null +++ b/src/bin/generator.rs @@ -0,0 +1,228 @@ +use clap::Parser; +use roto::proto_gen::google::protobuf::descriptor::{ + DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto, FileDescriptorSet, +}; +use std::fs; +use std::path::PathBuf; + +#[derive(Parser)] +#[command(author, version, about = "Generates Rust accessor and builder code from a protobuf descriptor set")] +struct Args { + /// Path to the descriptor set file (.desc) + #[arg(short, long)] + input: PathBuf, + + /// Path to the output Rust file (.rs) + #[arg(short, long)] + 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" + )); + } + } + + fs::write(&args.output, output)?; + Ok(()) +} diff --git a/src/proto_gen/google/protobuf/descriptor.rs b/src/proto_gen/google/protobuf/descriptor.rs index 5598ae4..142f751 100644 --- a/src/proto_gen/google/protobuf/descriptor.rs +++ b/src/proto_gen/google/protobuf/descriptor.rs @@ -254,6 +254,67 @@ impl<'b> DescriptorProtoBuilder<'b> { } } +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 enum_value(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(2) + } + + pub fn reserved_value(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(3) + } + + pub fn options(&self) -> crate::RepeatedFieldIterator<'a> { + self.0.iter_repeated(4) + } + + pub fn builder(buf: &mut [u8]) -> EnumDescriptorProtoBuilder<'_> { + EnumDescriptorProtoBuilder { + builder: ProtoBuilder::new(buf), + } + } +} + +pub struct EnumDescriptorProtoBuilder<'b> { + builder: ProtoBuilder<'b>, +} + +impl<'b> EnumDescriptorProtoBuilder<'b> { + pub fn name(mut self, value: &str) -> Result { + self.builder.write_string(1, value)?; + Ok(self) + } + + pub fn add_enum_value(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(2, data)?; + Ok(self) + } + + pub fn add_reserved_value(mut self, value: i32) -> Result { + self.builder.write_int32(3, value)?; + Ok(self) + } + + pub fn add_options(mut self, data: &[u8]) -> Result { + self.builder.write_bytes(4, data)?; + Ok(self) + } + + pub fn finish(self) -> Result<&'b mut [u8]> { + self.builder.finish() + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum FieldType { Double = 1,