From eddfa409335f48df422aa644c0cae8f9a84e4721 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 16 Apr 2023 14:41:04 -0400 Subject: [PATCH] Add higher-level code gen traits --- Cargo.lock | 2 +- nrpc-build/Cargo.toml | 2 +- nrpc-build/src/lib.rs | 2 ++ nrpc-build/src/token_gen_traits.rs | 48 ++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 nrpc-build/src/token_gen_traits.rs diff --git a/Cargo.lock b/Cargo.lock index 7421b97..049e796 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -290,7 +290,7 @@ dependencies = [ [[package]] name = "nrpc-build" -version = "0.3.0" +version = "0.4.0" dependencies = [ "nrpc", "prettyplease 0.2.4", diff --git a/nrpc-build/Cargo.toml b/nrpc-build/Cargo.toml index b0253c4..3b8859c 100644 --- a/nrpc-build/Cargo.toml +++ b/nrpc-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nrpc-build" -version = "0.3.0" +version = "0.4.0" edition = "2021" license = "Apache-2.0" repository = "https://github.com/NGnius/nRPC" diff --git a/nrpc-build/src/lib.rs b/nrpc-build/src/lib.rs index 206ab8f..0ac75dc 100644 --- a/nrpc-build/src/lib.rs +++ b/nrpc-build/src/lib.rs @@ -1,7 +1,9 @@ mod builder; mod preprocessor; mod service_gen; +mod token_gen_traits; pub use builder::{compile, compile_servers, compile_clients, Transpiler}; pub use preprocessor::Preprocessor; pub(crate) use service_gen::ProtobufServiceGenerator; +pub use token_gen_traits::{IServiceGenerator, IPreprocessor, AbstractImpl}; diff --git a/nrpc-build/src/token_gen_traits.rs b/nrpc-build/src/token_gen_traits.rs new file mode 100644 index 0000000..5d18a78 --- /dev/null +++ b/nrpc-build/src/token_gen_traits.rs @@ -0,0 +1,48 @@ +use prost_build::Service; +use prost_types::FileDescriptorSet; +use proc_macro2::TokenStream; + +/// Higher-level abstraction of prost_build::ServiceGenerator +pub trait IServiceGenerator { + fn generate(&mut self, service: Service) -> TokenStream; +} + +/// Higher-level abstraction of crate::Preprocessor +pub trait IPreprocessor { + fn process(&mut self, fds: &mut FileDescriptorSet) -> TokenStream; +} + +/// Low-level interop for high-level traits IServiceGenerator and IPreprocessor +pub struct AbstractImpl(X); + +impl AbstractImpl { + pub fn inner(self) -> X { + self.0 + } + + pub fn outer(value: X) -> Self { + Self(value) + } +} + +impl std::convert::From for AbstractImpl { + fn from(value: X) -> Self { + Self(value) + } +} + +impl prost_build::ServiceGenerator for AbstractImpl { + fn generate(&mut self, service: Service, buf: &mut String) { + let gen_code: syn::File = syn::parse2(self.0.generate(service)).expect("invalid tokenstream"); + let code_str = prettyplease::unparse(&gen_code); + buf.push_str(&code_str); + } +} + +impl super::Preprocessor for AbstractImpl { + fn process(&mut self, fds: &mut FileDescriptorSet, buf: &mut String) { + let gen_code: syn::File = syn::parse2(self.0.process(fds)).expect("invalid tokenstream"); + let code_str = prettyplease::unparse(&gen_code); + buf.push_str(&code_str); + } +}