Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rust-format crate with direct call to rustfmt #3369

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ ndarray = { version = "0.15.5", default-features = false }
proc-macro2 = "1.0"
quote = "1.0.9"
rayon = "1.5"
rust-format = { version = "0.3.4", features = ["token_stream"] }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
serde-aux = { version = "4.1.2", default-features = false }
Expand Down
58 changes: 32 additions & 26 deletions provider/datagen/src/databake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,39 @@ impl BakedDataExporter {
.with_extension(if is_expr { "rs.data" } else { "rs" });

let mut formatted = if self.pretty {
use rust_format::*;
RustFmt::from_config(
Config::new_str()
// We deal with line encoding later
.option("newline_style", "unix")
.option("normalize_doc_attributes", "true")
// Rustfmt silently gives up if it cannot achieve the max width, which happens for the root mod.rs
.option(
"max_width",
if relative_path.as_ref().as_os_str().to_str() == Some("mod") {
"150"
} else {
"100"
},
),
)
.format_tokens(if is_expr {
// Rustfmt cannot format Rust expressions, only full files. We need to wrap expressions in a main function
quote!(fn main() { #data })
use std::process::{Command, Stdio};
let mw = if relative_path.as_ref().as_os_str().to_str() == Some("mod") {
"max_width=150"
} else {
data
})
.map_err(|e| {
DataError::custom("Formatting error")
.with_display_context(&e)
.with_path_context(&path)
})?
"max_width=100"
};
let mut rustfmt = Command::new("rustfmt")
.arg("--config")
.arg("newline_style=unix")
.arg("--config")
.arg("normalize_doc_attributes=true")
.arg("--config")
.arg(mw)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()?;
let mut rustfmt_stdin = rustfmt.stdin.take().unwrap();
if is_expr {
write!(rustfmt_stdin, "fn main () {{ {data} }}")?
} else {
write!(rustfmt_stdin, "{data}")?
};

drop(rustfmt_stdin); // EOF

let output = rustfmt.wait_with_output()?;
if !output.status.success() {
let stderr = String::from_utf8(output.stderr)
.map_err(|_| DataError::custom("rustfmt output not utf-8"))?;
return Err(DataError::custom("rustfmt failed").with_display_context(&stderr));
}
String::from_utf8(output.stdout)
.map_err(|_| DataError::custom("rustfmt output not utf-8"))?
} else {
data.to_string()
};
Expand Down
2 changes: 1 addition & 1 deletion provider/testdata/data/baked/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tools/depcheck/src/allowlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ pub const EXTRA_DATAGEN_DEPS: &[&str] = &[
"num-integer",
"rawpointer",
"regex-syntax",
"rust-format",
"ryu",
"serde-aux",
"serde_json",
Expand Down