Add higher-level code gen traits
This commit is contained in:
parent
4677c02eab
commit
eddfa40933
4 changed files with 52 additions and 2 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -290,7 +290,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nrpc-build"
|
name = "nrpc-build"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nrpc",
|
"nrpc",
|
||||||
"prettyplease 0.2.4",
|
"prettyplease 0.2.4",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "nrpc-build"
|
name = "nrpc-build"
|
||||||
version = "0.3.0"
|
version = "0.4.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "Apache-2.0"
|
license = "Apache-2.0"
|
||||||
repository = "https://github.com/NGnius/nRPC"
|
repository = "https://github.com/NGnius/nRPC"
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
mod builder;
|
mod builder;
|
||||||
mod preprocessor;
|
mod preprocessor;
|
||||||
mod service_gen;
|
mod service_gen;
|
||||||
|
mod token_gen_traits;
|
||||||
|
|
||||||
pub use builder::{compile, compile_servers, compile_clients, Transpiler};
|
pub use builder::{compile, compile_servers, compile_clients, Transpiler};
|
||||||
pub use preprocessor::Preprocessor;
|
pub use preprocessor::Preprocessor;
|
||||||
pub(crate) use service_gen::ProtobufServiceGenerator;
|
pub(crate) use service_gen::ProtobufServiceGenerator;
|
||||||
|
pub use token_gen_traits::{IServiceGenerator, IPreprocessor, AbstractImpl};
|
||||||
|
|
48
nrpc-build/src/token_gen_traits.rs
Normal file
48
nrpc-build/src/token_gen_traits.rs
Normal file
|
@ -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>(X);
|
||||||
|
|
||||||
|
impl<X> AbstractImpl<X> {
|
||||||
|
pub fn inner(self) -> X {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn outer(value: X) -> Self {
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<X> std::convert::From<X> for AbstractImpl<X> {
|
||||||
|
fn from(value: X) -> Self {
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<X: IServiceGenerator> prost_build::ServiceGenerator for AbstractImpl<X> {
|
||||||
|
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<X: IPreprocessor> super::Preprocessor for AbstractImpl<X> {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue