@@ -1,12 +1,6 @@
|
||||
//! `tonic-build` compiles `proto` files via `prost` and generates service stubs
|
||||
//! and proto definitiones for use with `tonic`.
|
||||
//!
|
||||
//! # Features
|
||||
//!
|
||||
//! - `rustfmt`: This feature enables the use of `rustfmt` to format the output code
|
||||
//! this makes the code readable and the error messages nice. This requires that `rustfmt`
|
||||
//! is installed. This is enabled by default.
|
||||
//!
|
||||
//! # Required dependencies
|
||||
//!
|
||||
//! ```toml
|
||||
@@ -85,13 +79,6 @@ mod prost;
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "prost")))]
|
||||
pub use prost::{compile_protos, configure, Builder};
|
||||
|
||||
#[cfg(feature = "rustfmt")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
|
||||
use std::io::{self, Write};
|
||||
#[cfg(feature = "rustfmt")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
|
||||
use std::process::{exit, Command};
|
||||
|
||||
/// Service code generation for client
|
||||
pub mod client;
|
||||
/// Service code generation for Server
|
||||
@@ -217,42 +204,6 @@ fn generate_attributes<'a>(
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
/// Format files under the out_dir with rustfmt
|
||||
#[cfg(feature = "rustfmt")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "rustfmt")))]
|
||||
pub fn fmt(out_dir: &str) {
|
||||
let dir = std::fs::read_dir(out_dir).unwrap();
|
||||
|
||||
for entry in dir {
|
||||
let file = entry.unwrap().file_name().into_string().unwrap();
|
||||
if !file.ends_with(".rs") {
|
||||
continue;
|
||||
}
|
||||
let result =
|
||||
Command::new(std::env::var("RUSTFMT").unwrap_or_else(|_| "rustfmt".to_owned()))
|
||||
.arg("--emit")
|
||||
.arg("files")
|
||||
.arg("--edition")
|
||||
.arg("2018")
|
||||
.arg(format!("{}/{}", out_dir, file))
|
||||
.output();
|
||||
|
||||
match result {
|
||||
Err(e) => {
|
||||
eprintln!("error running rustfmt: {:?}", e);
|
||||
exit(1)
|
||||
}
|
||||
Ok(output) => {
|
||||
if !output.status.success() {
|
||||
io::stdout().write_all(&output.stdout).unwrap();
|
||||
io::stderr().write_all(&output.stderr).unwrap();
|
||||
exit(output.status.code().unwrap_or(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate a singular line of a doc comment
|
||||
fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
|
||||
let mut doc_stream = TokenStream::new();
|
||||
|
||||
@@ -22,8 +22,6 @@ pub fn configure() -> Builder {
|
||||
client_attributes: Attributes::default(),
|
||||
proto_path: "super".to_string(),
|
||||
compile_well_known_types: false,
|
||||
#[cfg(feature = "rustfmt")]
|
||||
format: true,
|
||||
emit_package: true,
|
||||
protoc_args: Vec::new(),
|
||||
include_file: None,
|
||||
@@ -184,7 +182,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
|
||||
#clients
|
||||
};
|
||||
|
||||
let code = format!("{}", client_service);
|
||||
let ast: syn::File = syn::parse2(client_service).expect("not a valid tokenstream");
|
||||
let code = prettyplease::unparse(&ast);
|
||||
buf.push_str(&code);
|
||||
|
||||
self.clients = TokenStream::default();
|
||||
@@ -197,7 +196,8 @@ impl prost_build::ServiceGenerator for ServiceGenerator {
|
||||
#servers
|
||||
};
|
||||
|
||||
let code = format!("{}", server_service);
|
||||
let ast: syn::File = syn::parse2(server_service).expect("not a valid tokenstream");
|
||||
let code = prettyplease::unparse(&ast);
|
||||
buf.push_str(&code);
|
||||
|
||||
self.servers = TokenStream::default();
|
||||
@@ -223,8 +223,6 @@ pub struct Builder {
|
||||
pub(crate) include_file: Option<PathBuf>,
|
||||
|
||||
out_dir: Option<PathBuf>,
|
||||
#[cfg(feature = "rustfmt")]
|
||||
format: bool,
|
||||
}
|
||||
|
||||
impl Builder {
|
||||
@@ -247,13 +245,6 @@ impl Builder {
|
||||
self
|
||||
}
|
||||
|
||||
/// Enable the output to be formated by rustfmt.
|
||||
#[cfg(feature = "rustfmt")]
|
||||
pub fn format(mut self, run: bool) -> Self {
|
||||
self.format = run;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the output directory to generate code to.
|
||||
///
|
||||
/// Defaults to the `OUT_DIR` environment variable.
|
||||
@@ -397,9 +388,6 @@ impl Builder {
|
||||
PathBuf::from(std::env::var("OUT_DIR").unwrap())
|
||||
};
|
||||
|
||||
#[cfg(feature = "rustfmt")]
|
||||
let format = self.format;
|
||||
|
||||
config.out_dir(out_dir.clone());
|
||||
if let Some(path) = self.file_descriptor_set_path.as_ref() {
|
||||
config.file_descriptor_set_path(path);
|
||||
@@ -428,13 +416,6 @@ impl Builder {
|
||||
|
||||
config.compile_protos(protos, includes)?;
|
||||
|
||||
#[cfg(feature = "rustfmt")]
|
||||
{
|
||||
if format {
|
||||
super::fmt(out_dir.to_str().expect("Expected utf8 out_dir"));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user