Initial commit of AI generated slop
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::google::protobuf::descriptor::{
|
||||
DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto,
|
||||
FileDescriptorSet, MessageOptions, OneofDescriptorProto,
|
||||
FileDescriptorSet, MessageOptions, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto,
|
||||
};
|
||||
use roto_runtime::ProtoAccessor;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
@@ -434,6 +434,25 @@ 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!("pub struct Owned{} {{\n", msg_name));
|
||||
output.push_str(" pub data: bytes::Bytes,\n");
|
||||
output.push_str("}\n\n");
|
||||
|
||||
output.push_str(&format!("impl roto_runtime::RotoOwned for Owned{} {{\n", msg_name));
|
||||
output.push_str(&format!(" fn reader(&self) -> {}<'_> {{\n", msg_name));
|
||||
output.push_str(&format!(" {}::new(&self.data).expect(\"failed to create reader\")\n", msg_name));
|
||||
output.push_str(" }\n");
|
||||
output.push_str("}\n\n");
|
||||
|
||||
output.push_str(&format!("impl roto_runtime::RotoMessage for Owned{} {{\n", msg_name));
|
||||
output.push_str(" fn decode(buf: bytes::Bytes) -> Self {\n");
|
||||
output.push_str(&format!(" Owned{} {{ data: buf }}\n", msg_name));
|
||||
output.push_str(" }\n\n");
|
||||
output.push_str(" fn bytes(&self) -> bytes::Bytes {\n");
|
||||
output.push_str(" self.data.clone()\n");
|
||||
output.push_str(" }\n");
|
||||
output.push_str("}\n\n");
|
||||
|
||||
let mut nested_enums = Vec::new();
|
||||
for e_res in msg_proto.enum_type() {
|
||||
if let Ok((e, _)) = e_res {
|
||||
@@ -524,9 +543,13 @@ pub fn generate_rust_code(
|
||||
|
||||
let mut output = String::new();
|
||||
output.push_str("// @generated by protoc-gen-roto — do not edit\n");
|
||||
output.push_str("#![allow(unused_imports)]\n\n");
|
||||
output.push_str("#[allow(unused_imports)]\n\n");
|
||||
output.push_str("use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator};\n");
|
||||
output.push_str("use std::str;\n\n");
|
||||
output.push_str("use std::str;\n");
|
||||
output.push_str("use bytes::Bytes;\n");
|
||||
output.push_str("use tonic::{Request, Response, Status};\n");
|
||||
output.push_str("use tokio_stream::Stream;\n");
|
||||
output.push_str("use std::pin::Pin;\n\n");
|
||||
|
||||
for dep_res in file_proto.dependency() {
|
||||
let (dep_data, _) = dep_res.expect("Failed to iterate dependency");
|
||||
@@ -553,6 +576,15 @@ pub fn generate_rust_code(
|
||||
&mut output,
|
||||
);
|
||||
}
|
||||
|
||||
// Services
|
||||
for svc_res in file_proto.service() {
|
||||
let (svc_data, _) = svc_res.expect("Failed to iterate service");
|
||||
write_service(
|
||||
&ServiceDescriptorProto::new(svc_data).expect("Failed to parse ServiceDescriptorProto"),
|
||||
&mut output,
|
||||
);
|
||||
}
|
||||
generated_files.push((rust_file_name, output));
|
||||
}
|
||||
|
||||
@@ -608,3 +640,44 @@ pub fn generate_rust_code(
|
||||
|
||||
generated_files
|
||||
}
|
||||
|
||||
fn write_service(svc_proto: &ServiceDescriptorProto, output: &mut String) {
|
||||
let svc_name = to_pascal_case(svc_proto.name().unwrap());
|
||||
output.push_str(&format!("#[tonic::async_trait]\npub trait {}: Send + Sync + 'static {{\n", svc_name));
|
||||
|
||||
for method_res in svc_proto.method() {
|
||||
let (method_data, _) = method_res.expect("Failed to iterate method");
|
||||
let method_proto = MethodDescriptorProto::new(method_data).expect("Failed to parse MethodDescriptorProto");
|
||||
let method_name = to_snake_case(method_proto.name().unwrap());
|
||||
|
||||
let input_full_name = method_proto.input_type().unwrap();
|
||||
let output_full_name = method_proto.output_type().unwrap();
|
||||
|
||||
let input_type = input_full_name.split('.').last().unwrap();
|
||||
let output_type = output_full_name.split('.').last().unwrap();
|
||||
|
||||
let input_owned = format!("Owned{}", input_type);
|
||||
let output_owned = format!("Owned{}", output_type);
|
||||
|
||||
let client_streaming = method_proto.client_streaming().unwrap_or(false);
|
||||
let server_streaming = method_proto.server_streaming().unwrap_or(false);
|
||||
|
||||
let req_type = if client_streaming {
|
||||
format!("Request<tonic::Streaming<{}>>", input_owned)
|
||||
} else {
|
||||
format!("Request<{}>", input_owned)
|
||||
};
|
||||
|
||||
let resp_type = if server_streaming {
|
||||
format!("Response<Pin<Box<dyn Stream<Item = Result<{}, Status>> + Send>>>", output_owned)
|
||||
} else {
|
||||
format!("Response<{}>", output_owned)
|
||||
};
|
||||
|
||||
output.push_str(&format!(
|
||||
" async fn {}(&self, request: {}) -> Result<{}, Status>;\n",
|
||||
method_name, req_type, resp_type
|
||||
));
|
||||
}
|
||||
output.push_str("}\n\n");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user