Why doesn't rustfmt format this vec![] of overlong constructors? #6547
-
I have this code for a semantic validation rules engine I'm developing pub struct Rule<'a, F>
where
F: Fn(&'a serde_json::Value) -> Option<EvaluationResult>,
{
pub name: &'a str,
pub description: &'a str,
pub evaluate: F,
}
impl<'a, F> Rule<'a, F>
where
F: Fn(&'a serde_json::Value) -> Option<EvaluationResult>,
{
fn new(name: &'a str, description: &'a str, evaluate: F) -> Self {
Self {
name,
description,
evaluate,
}
}
}
pub fn rules<'a>() -> Vec<Rule<'a, fn(&'a serde_json::Value) -> Option<EvaluationResult>>> {
vec![
Rule::new(
"header-paid-should-be-sum-of-payments",
"The sum of all payments should always equal the amount in the header's 'paid' field",
header_paid_should_be_sum_of_payments,
),
Rule::new("all-headers-should-have-tz", "All receipts should include a timezone that best represents either the customer's physical location at the time of purchase, or the merchant's region of business", all_headers_should_have_tz),
Rule::new("flight_fare-at-ticket-or-segment-exclusively", "Flight receipts should set the fare at the ticket level, or the segment level, but not both", flight_fare_at_ticket_or_segment_exclusively),Rule::new("header-total-should-be-sum-of-all-line-items-taxes-and-adjustments", "The header total should equal the sum of all line items, taxes, and adjustments", header_total_should_be_sum_of_all_line_items_taxes_and_adjustments),
]
} ...and I spent half an hour wondering what was wrong with my git hooks... or cargo fmt... or what, until I finally realized Can someone help me understand why this is? I would expect something like this as output: pub fn rules<'a>() -> Vec<Rule<'a, fn(&'a serde_json::Value) -> Option<EvaluationResult>>> {
vec![
Rule::new(
"header-paid-should-be-sum-of-payments",
"The sum of all payments should always equal the amount in the header's 'paid' field",
header_paid_should_be_sum_of_payments,
),
Rule::new(
"all-headers-should-have-tz",
"All receipts should include a timezone that best represents either the customer's physical location at the time of purchase, or the merchant's region of business",
all_headers_should_have_tz
),
Rule::new(
"flight_fare-at-ticket-or-segment-exclusively",
"Flight receipts should set the fare at the ticket level, or the segment level, but not both",
flight_fare_at_ticket_or_segment_exclusively,
),
Rule::new(
"header-total-should-be-sum-of-all-line-items-taxes-and-adjustments",
"The header total should equal the sum of all line items, taxes, and adjustments",
header_total_should_be_sum_of_all_line_items_taxes_and_adjustments,
)
]
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Because of the one very long string that's too long to possibly represent as a single-line string within the user-controllable max width limit. In this type of context rustfmt will simply defer to the human to format.
If you'd like to get rustfmt to be able to format these then you can either enable the |
Beta Was this translation helpful? Give feedback.
Because of the one very long string that's too long to possibly represent as a single-line string within the user-controllable max width limit. In this type of context rustfmt will simply defer to the human to format.
"All receipts should include a timezone that best represents either the customer's physical location at the time of purchase, or the merchant's region of business",
If you'd like to get rustfmt to be able to format these then you can either enable the
format_strings
config option to give it permission to wrap the string on your behalf, or manually split the string across multiple lines