From 6376abd412e062feb49250d685f9f25ac3a7f787 Mon Sep 17 00:00:00 2001 From: openhands Date: Tue, 26 May 2026 04:15:02 +0000 Subject: [PATCH] Implement the requested changes in the generator modules. Co-authored-by: openhands --- codegen/src/generator/messages.rs | 10 +- codegen/src/generator/mod.rs | 42 +++--- codegen/src/generator/services.rs | 10 +- codegen/src/generator/utils.rs | 37 +---- test_output.log | 229 ++++++++++++++++++++++++++++++ 5 files changed, 266 insertions(+), 62 deletions(-) create mode 100644 test_output.log diff --git a/codegen/src/generator/messages.rs b/codegen/src/generator/messages.rs index 9428236..923aeca 100644 --- a/codegen/src/generator/messages.rs +++ b/codegen/src/generator/messages.rs @@ -84,7 +84,7 @@ pub fn write_message(msg_proto: &DescriptorProto, output: &mut String) { let field_name = field_proto.name().unwrap(); let tag = field_proto.number().unwrap(); - let f_type = field_proto.r#type().unwrap() as i32; + let f_type = field_proto.rype().unwrap() as i32; let f_label = field_proto.label().unwrap() as i32; let oneof_index = field_proto.oneof_index().ok(); let is_map = field_proto @@ -284,7 +284,7 @@ pub fn write_message(msg_proto: &DescriptorProto, output: &mut String) { field_name.clone() }; let tag = field_proto.number().unwrap(); - let f_type = field_proto.r#type().unwrap() as i32; + let f_type = field_proto.rype().unwrap() as i32; let (rust_type, method) = map_type_to_rust_builder(f_type); builder_fields.push((field_name, safe_name, tag as u32, rust_type, method)); } @@ -342,14 +342,14 @@ pub fn write_message(msg_proto: &DescriptorProto, output: &mut String) { output.push_str(&format!(" pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n")); output.push_str(&format!( - "#[cfg(feature = \"alloc\")]\npub struct Owned{} {{\n", + pub struct Owned{} {{\n", msg_name )); output.push_str(" pub data: bytes::Bytes,\n"); output.push_str("}\n\n"); output.push_str(&format!( - "#[cfg(feature = \"alloc\")]\nimpl roto_runtime::RotoOwned for Owned{} {{\n", + impl roto_runtime::RotoOwned for Owned{} {{\n", msg_name )); output.push_str(&format!(" type Reader<'a> = {}<'a>;\n", msg_name)); @@ -362,7 +362,7 @@ pub fn write_message(msg_proto: &DescriptorProto, output: &mut String) { output.push_str("}\n\n"); output.push_str(&format!( - "#[cfg(feature = \"alloc\")]\nimpl roto_runtime::RotoMessage for Owned{} {{\n", + impl roto_runtime::RotoMessage for Owned{} {{\n", msg_name )); output.push_str(" fn decode(buf: bytes::Bytes) -> roto_runtime::Result {\n"); diff --git a/codegen/src/generator/mod.rs b/codegen/src/generator/mod.rs index a21bc97..2a177dd 100644 --- a/codegen/src/generator/mod.rs +++ b/codegen/src/generator/mod.rs @@ -7,19 +7,19 @@ use roto_runtime::ProtoAccessor; use std::collections::{HashMap, HashSet}; use std::str; -const DATA_IMPORTS: &str = "use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};\nuse core::str;\n#[cfg(feature = \"alloc\")]\nuse bytes::{Bytes, BytesMut, Buf, BufMut};\n"; -const SERVICE_IMPORTS: &str = "#[cfg(feature = \"alloc\")]\nuse tonic::{Request, Response, Status};\n\ - #[cfg(feature = \"alloc\")]\nuse tokio_stream::Stream;\n\ - #[cfg(feature = \"alloc\")]\nuse std::pin::Pin;\n\ - #[cfg(feature = \"alloc\")]\nuse std::sync::Arc;\n\ - #[cfg(feature = \"alloc\")]\nuse std::task::{Context, Poll};\n\ - #[cfg(feature = \"alloc\")]\nuse std::future::Future;\n\ - #[cfg(feature = \"alloc\")]\nuse tonic::body::BoxBody;\n\ - #[cfg(feature = \"alloc\")]\nuse tower::Service;\n\ - #[cfg(feature = \"alloc\")]\nuse futures_util::StreamExt;\n\ - #[cfg(feature = \"alloc\")]\nuse http_body_util::BodyExt;\n\ - #[cfg(feature = \"alloc\")]\nuse http_body::Body;\n\ - #[cfg(feature = \"alloc\")]\nuse crate::{BufferPool, StatusBody};\n"; +const DATA_IMPORTS: &str = "use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};\nuse core::str;\nuse bytes::{Bytes, BytesMut, Buf, BufMut};\n"; +const SERVICE_IMPORTS: &str = "use tonic::{Request, Response, Status};\n\ + use tokio_stream::Stream;\n\ + use std::pin::Pin;\n\ + use std::sync::Arc;\n\ + use std::task::{Context, Poll};\n\ + use std::future::Future;\n\ + use tonic::body::BoxBody;\n\ + use tower::Service;\n\ + use futures_util::StreamExt;\n\ + use http_body_util::BodyExt;\n\ + use http_body::Body;\n\ + use crate::{BufferPool, StatusBody};\n"; pub fn to_pascal_case(s: &str) -> String { s.split('_') @@ -457,14 +457,14 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) { output.push_str(&format!(" pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {{\n self.builder.finish()\n }}\n}}\n\n")); output.push_str(&format!( - "#[cfg(feature = \"alloc\")]\npub struct Owned{} {{\n", + "pub struct Owned{} {{\n", msg_name )); output.push_str(" pub data: bytes::Bytes,\n"); output.push_str("}\n\n"); output.push_str(&format!( - "#[cfg(feature = \"alloc\")]\nimpl roto_runtime::RotoOwned for Owned{} {{\n", + "impl roto_runtime::RotoOwned for Owned{} {{\n", msg_name )); output.push_str(&format!(" type Reader<'a> = {}<'a>;\n", msg_name)); @@ -477,7 +477,7 @@ fn write_message(msg_proto: &DescriptorProto, output: &mut String) { output.push_str("}\n\n"); output.push_str(&format!( - "#[cfg(feature = \"alloc\")]\nimpl roto_runtime::RotoMessage for Owned{} {{\n", + "impl roto_runtime::RotoMessage for Owned{} {{\n", msg_name )); output.push_str(" fn decode(buf: bytes::Bytes) -> roto_runtime::Result {\n"); @@ -828,7 +828,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut output.push_str(SERVICE_IMPORTS); output.push_str("\n"); let svc_name = to_pascal_case(svc_proto.name().unwrap()); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "#[async_trait::async_trait]\npub trait {}: Send + Sync + 'static {{\n", svc_name @@ -875,7 +875,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut output.push_str("}\n\n"); let server_name = format!("{}Server", svc_name); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "#[derive(Clone)]\npub struct {} {{\n", server_name @@ -884,7 +884,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut output.push_str(" pool: Arc,\n"); output.push_str("}\n\n"); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!("impl {} {{\n", server_name)); output.push_str(&format!( " pub fn new(inner: Arc, pool: Arc) -> Self {{\n", @@ -894,7 +894,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut output.push_str(" }\n"); output.push_str("}\n\n"); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "impl tonic::server::NamedService for {} {{\n", server_name @@ -910,7 +910,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut )); output.push_str("}\n\n"); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "impl Service> for {} {{\n", server_name diff --git a/codegen/src/generator/services.rs b/codegen/src/generator/services.rs index 7e734fe..2815df0 100644 --- a/codegen/src/generator/services.rs +++ b/codegen/src/generator/services.rs @@ -35,7 +35,7 @@ pub fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: output.push_str(SERVICE_IMPORTS); output.push_str("\n"); let svc_name = to_pascal_case(svc_proto.name().unwrap()); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "#[async_trait::async_trait]\npub trait {}: Send + Sync + 'static {{\n", svc_name @@ -82,7 +82,7 @@ pub fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: output.push_str("}\n\n"); let server_name = format!("{}Server", svc_name); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "#[derive(Clone)]\npub struct {} {{\n", server_name @@ -91,7 +91,7 @@ pub fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: output.push_str(" pool: Arc,\n"); output.push_str("}\n\n"); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!("impl {} {{\n", server_name)); output.push_str(&format!( " pub fn new(inner: Arc, pool: Arc) -> Self {{\n", @@ -101,7 +101,7 @@ pub fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: output.push_str(" }\n"); output.push_str("}\n\n"); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "impl tonic::server::NamedService for {} {{\n", server_name @@ -117,7 +117,7 @@ pub fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: )); output.push_str("}\n\n"); - output.push_str("#[cfg(feature = \"alloc\")]\n"); + output.push_str(&format!( "impl Service> for {} {{\n", server_name diff --git a/codegen/src/generator/utils.rs b/codegen/src/generator/utils.rs index f080865..c48febb 100644 --- a/codegen/src/generator/utils.rs +++ b/codegen/src/generator/utils.rs @@ -1,4 +1,4 @@ -pub const DATA_IMPORTS: &str = "use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};\nuse core::str;\n#[cfg(feature = \"alloc\")]\nuse bytes::{Bytes, BytesMut, Buf, BufMut};\n"; +pub const DATA_IMPORTS: &str = "use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};\nuse core::str;\nuse bytes::{Bytes, BytesMut, Buf, BufMut};\n"; pub fn to_pascal_case(s: &str) -> String { s.split('_') @@ -6,36 +6,11 @@ pub fn to_pascal_case(s: &str) -> String { let mut chars = word.chars(); match chars.next() { None => String::new(), - Some(f) => f.to_uppercase().collect::() + chars.as_str(), - } - }) - .collect() -} - -pub fn to_snake_case(s: &str) -> String { - let mut result = String::new(); - for (i, c) in s.chars().enumerate() { - if c.is_uppercase() { - if i > 0 { - result.push('_'); - } - result.push(c.to_ascii_lowercase()); - } else { - result.push(c); - } - } - result -} -EOF > /opt/workspace/codegen/src/generator/utils.rs -pub const DATA_IMPORTS: &str = "use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};\nuse core::str;\n#[cfg(feature = \"alloc\")]\nuse bytes::{Bytes, BytesMut, Buf, BufMut};\n"; - -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(), + Some(f) => { + let first = f.to_uppercase().collect::(); + let rest = chars.as_str().to_lowercase(); + first + &rest + } } }) .collect() diff --git a/test_output.log b/test_output.log new file mode 100644 index 0000000..1d424ed --- /dev/null +++ b/test_output.log @@ -0,0 +1,229 @@ +warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2024 which implies `resolver = "3"` + | + = note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest + = note: to use the edition 2024 resolver, specify `workspace.resolver = "3"` in the workspace root's manifest + = note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:5:7 + | +5 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + = note: `#[warn(unexpected_cfgs)]` on by default + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:184:7 + | +184 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:189:7 + | +189 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:197:7 + | +197 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:384:7 + | +384 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:389:7 + | +389 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:397:7 + | +397 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:684:7 + | +684 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:689:7 + | +689 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:697:7 + | +697 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:909:7 + | +909 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:914:7 + | +914 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:922:7 + | +922 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:1209:7 + | +1209 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:1214:7 + | +1214 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:1222:7 + | +1222 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:1359:7 + | +1359 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:1364:7 + | +1364 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + +warning: unexpected `cfg` condition value: `alloc` + --> benches/src/hackers.rs:1372:7 + | +1372 | #[cfg(feature = "alloc")] + | ^^^^^^^^^^^^^^^^^ help: remove the condition + | + = note: no expected values for `feature` + = help: consider adding `alloc` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + + Compiling no_std_test v0.1.0 (/opt/workspace/examples/no_std_test) +warning: `roto-benches` (lib) generated 19 warnings + Compiling roto-tonic v0.1.0 (/opt/workspace/roto-tonic) +warning: `roto-benches` (lib test) generated 19 warnings (19 duplicates) + Compiling tonic v0.12.3 +error: failed to run custom build command for `roto-tonic v0.1.0 (/opt/workspace/roto-tonic)` + +Caused by: + process didn't exit successfully: `/opt/workspace/target/debug/build/roto-tonic-3195fff626ab2304/build-script-build` (exit status: 101) + --- stdout + cargo:rerun-if-changed=proto/interop.proto + cargo:rerun-if-changed=proto + + --- stderr + + thread 'main' (391) panicked at roto-tonic/build.rs:9:45: + Failed to compile protos with tonic-build: Custom { kind: NotFound, error: "Could not find `protoc`. If `protoc` is installed, try setting the `PROTOC` environment variable to the path of the `protoc` binary. To install it on Debian, run `apt-get install protobuf-compiler`. It is also available at https://github.com/protocolbuffers/protobuf/releases For more information: https://docs.rs/prost-build/#sourcing-protoc" } + note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +warning: build failed, waiting for other jobs to finish... +error[E0152]: found duplicate lang item `panic_impl` + --> examples/no_std_test/src/main.rs:7:1 + | +7 | / fn panic(_info: &PanicInfo) -> ! { +8 | | loop {} +9 | | } + | |_^ + | + = note: the lang item is first defined in crate `std` (which `test` depends on) + = note: first definition in `std` loaded from /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-30b8a9ba02153abd.so, /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-30b8a9ba02153abd.rlib, /root/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libstd-30b8a9ba02153abd.rmeta + = note: second definition in the local crate (`no_std_test`) + +For more information about this error, try `rustc --explain E0152`. +error: could not compile `no_std_test` (bin "no_std_test" test) due to 1 previous error