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

Switch datagen over to prettyplease #3363

Closed
wants to merge 3 commits into from
Closed
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 11 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion provider/datagen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ log = "0.4"
ndarray = { version = "0.15.5", default-features = false }
proc-macro2 = "1.0"
quote = "1.0.9"
prettyplease = "0.1.0"
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
33 changes: 8 additions & 25 deletions provider/datagen/src/databake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,15 @@ 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
let data = if is_expr {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think prettyplease can work on arbitrary tokens.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it needs a syn::File. I tried that, it panics.

quote!(fn main() { #data })
} else {
data
})
.map_err(|e| {
DataError::custom("Formatting error")
.with_display_context(&e)
.with_path_context(&path)
})?
};
prettyplease::unparse(
&syn::parse2(data)
.map_err(|e| DataError::custom("syn parsing").with_display_context(&e))?,
)
} else {
data.to_string()
};
Expand All @@ -132,7 +114,7 @@ impl BakedDataExporter {
.replace("icu_", "icu::")
.replace("icu::provider", "icu_provider");
}

let formatted = if self.pretty && is_expr {
formatted = formatted.replace("\n ", "\n");
formatted
Expand All @@ -143,6 +125,7 @@ impl BakedDataExporter {
} else {
&formatted
};

std::fs::create_dir_all(path.parent().unwrap())?;
let mut file = crlify::BufWriterWithLineEndingFix::new(
File::create(&path).map_err(|e| DataError::from(e).with_path_context(&path))?,
Expand Down
3 changes: 1 addition & 2 deletions provider/testdata/data/baked/calendar/japanese_v1/mod.rs

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

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

2 changes: 1 addition & 1 deletion provider/testdata/data/baked/calendar/japanext_v1/mod.rs

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

Large diffs are not rendered by default.

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

20 changes: 15 additions & 5 deletions provider/testdata/data/baked/collator/data_v1/ar.rs.data

Large diffs are not rendered by default.

Loading