|
|
|
@@ -7,19 +7,23 @@ use crate::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;\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";
|
|
|
|
|
const DATA_IMPORTS: &str = "#[allow(unused)]\n\
|
|
|
|
|
use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};\n\
|
|
|
|
|
use core::str;\n\
|
|
|
|
|
use bytes::{Bytes, BytesMut, Buf, BufMut};\n";
|
|
|
|
|
const SERVICE_IMPORTS: &str = "#[allow(unused)]\n\
|
|
|
|
|
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('_')
|
|
|
|
@@ -456,10 +460,7 @@ 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(&format!("pub struct Owned{} {{\n", msg_name));
|
|
|
|
|
output.push_str(" pub data: bytes::Bytes,\n");
|
|
|
|
|
output.push_str("}\n\n");
|
|
|
|
|
|
|
|
|
@@ -828,7 +829,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(&format!(
|
|
|
|
|
"#[async_trait::async_trait]\npub trait {}: Send + Sync + 'static {{\n",
|
|
|
|
|
svc_name
|
|
|
|
@@ -875,7 +876,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(&format!(
|
|
|
|
|
"#[derive(Clone)]\npub struct {} {{\n",
|
|
|
|
|
server_name
|
|
|
|
@@ -884,7 +885,6 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut
|
|
|
|
|
output.push_str(" pool: Arc<BufferPool>,\n");
|
|
|
|
|
output.push_str("}\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.push_str(&format!("impl {} {{\n", server_name));
|
|
|
|
|
output.push_str(&format!(
|
|
|
|
|
" pub fn new(inner: Arc<dyn {}>, pool: Arc<BufferPool>) -> Self {{\n",
|
|
|
|
@@ -894,7 +894,6 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut
|
|
|
|
|
output.push_str(" }\n");
|
|
|
|
|
output.push_str("}\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.push_str(&format!(
|
|
|
|
|
"impl tonic::server::NamedService for {} {{\n",
|
|
|
|
|
server_name
|
|
|
|
@@ -910,7 +909,6 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut
|
|
|
|
|
));
|
|
|
|
|
output.push_str("}\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.push_str(&format!(
|
|
|
|
|
"impl Service<http::Request<BoxBody>> for {} {{\n",
|
|
|
|
|
server_name
|
|
|
|
@@ -946,7 +944,8 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut
|
|
|
|
|
output.push_str(" return Ok(http::Response::builder().status(200).body(res_body).unwrap());\n");
|
|
|
|
|
output.push_str(" }\n\n");
|
|
|
|
|
output.push_str(" let payload = bytes_vec.slice(5..);\n");
|
|
|
|
|
output.push_str(" let mut routed = false;\n\n");
|
|
|
|
|
output.push_str(" #[allow(unused_assignments)]\n");
|
|
|
|
|
output.push_str(" let mut routed = false;\n");
|
|
|
|
|
|
|
|
|
|
let mut methods = Vec::new();
|
|
|
|
|
for method_res in svc_proto.method() {
|
|
|
|
@@ -1003,7 +1002,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut
|
|
|
|
|
input_owned
|
|
|
|
|
));
|
|
|
|
|
output.push_str(" Ok(msg) => msg,\n");
|
|
|
|
|
output.push_str(" Err(e) => {\n");
|
|
|
|
|
output.push_str(" Err(_e) => {\n");
|
|
|
|
|
output.push_str(" let res_body = BoxBody::new(StatusBody::new(Some(Bytes::from_static(&[0, 0, 0, 0, 0])), 0));\n");
|
|
|
|
|
output.push_str(" return Ok(http::Response::builder().status(200).body(res_body).unwrap());\n");
|
|
|
|
|
output.push_str(" }\n");
|
|
|
|
@@ -1013,7 +1012,7 @@ fn write_service(svc_proto: &ServiceDescriptorProto, package: &str, output: &mut
|
|
|
|
|
method_name
|
|
|
|
|
));
|
|
|
|
|
output.push_str(" Ok(res) => res,\n");
|
|
|
|
|
output.push_str(" Err(e) => {\n");
|
|
|
|
|
output.push_str(" Err(_e) => {\n");
|
|
|
|
|
output.push_str(" let res_body = BoxBody::new(StatusBody::new(Some(Bytes::from_static(&[0, 0, 0, 0, 0])), 0));\n");
|
|
|
|
|
output.push_str(" return Ok(http::Response::builder().status(200).body(res_body).unwrap());\n");
|
|
|
|
|
output.push_str(" }\n");
|
|
|
|
|