1 use std::collections::{hash_set, HashSet};
3 use std::path::{Path, PathBuf};
6 use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
8 use crate::config::config_type::ConfigType;
9 use crate::config::lists::*;
10 use crate::config::Config;
12 /// Macro that will stringify the enum variants or a provided textual repr
14 macro_rules! configuration_option_enum_stringify {
19 ($_variant:ident: $value:expr) => {
24 /// Macro for deriving implementations of Serialize/Deserialize for enums
26 macro_rules! impl_enum_serialize_and_deserialize {
27 ( $e:ident, $( $variant:ident $(: $value:expr)* ),* ) => {
28 impl ::serde::ser::Serialize for $e {
29 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30 where S: ::serde::ser::Serializer
32 use serde::ser::Error;
34 // We don't know whether the user of the macro has given us all options.
35 #[allow(unreachable_patterns)]
38 $e::$variant => serializer.serialize_str(
39 configuration_option_enum_stringify!($variant $(: $value)*)
43 Err(S::Error::custom(format!("Cannot serialize {:?}", self)))
49 impl<'de> ::serde::de::Deserialize<'de> for $e {
50 fn deserialize<D>(d: D) -> Result<Self, D::Error>
51 where D: ::serde::Deserializer<'de> {
52 use serde::de::{Error, Visitor};
53 use std::marker::PhantomData;
55 struct StringOnly<T>(PhantomData<T>);
56 impl<'de, T> Visitor<'de> for StringOnly<T>
57 where T: ::serde::Deserializer<'de> {
59 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
60 formatter.write_str("string")
62 fn visit_str<E>(self, value: &str) -> Result<String, E> {
63 Ok(String::from(value))
66 let s = d.deserialize_string(StringOnly::<D>(PhantomData))?;
68 if configuration_option_enum_stringify!($variant $(: $value)*)
69 .eq_ignore_ascii_case(&s) {
70 return Ok($e::$variant);
73 static ALLOWED: &'static[&str] = &[
74 $(configuration_option_enum_stringify!($variant $(: $value)*),)*];
75 Err(D::Error::unknown_variant(&s, ALLOWED))
79 impl ::std::str::FromStr for $e {
80 type Err = &'static str;
82 fn from_str(s: &str) -> Result<Self, Self::Err> {
84 if configuration_option_enum_stringify!($variant $(: $value)*)
85 .eq_ignore_ascii_case(s) {
86 return Ok($e::$variant);
93 impl ConfigType for $e {
94 fn doc_hint() -> String {
95 let mut variants = Vec::new();
98 configuration_option_enum_stringify!($variant $(: $value)*)
101 format!("[{}]", variants.join("|"))
107 macro_rules! configuration_option_enum {
108 ($e:ident: $( $name:ident $(: $value:expr)* ),+ $(,)*) => (
109 #[derive(Copy, Clone, Eq, PartialEq)]
114 impl ::std::fmt::Debug for $e {
115 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
116 f.write_str(match self {
118 $e::$name => configuration_option_enum_stringify!($name $(: $value)*),
124 impl_enum_serialize_and_deserialize!($e, $( $name $(: $value)* ),+);
128 configuration_option_enum! { NewlineStyle:
129 Auto, // Auto-detect based on the raw source input
132 Native, // \r\n in Windows, \n on other platforms
136 fn auto_detect(raw_input_text: &str) -> NewlineStyle {
137 if let Some(pos) = raw_input_text.find('\n') {
138 let pos = pos.saturating_sub(1);
139 if let Some('\r') = raw_input_text.chars().nth(pos) {
140 NewlineStyle::Windows
149 fn native() -> NewlineStyle {
151 NewlineStyle::Windows
157 /// Apply this newline style to the formatted text. When the style is set
158 /// to `Auto`, the `raw_input_text` is used to detect the existing line
161 /// If the style is set to `Auto` and `raw_input_text` contains no
162 /// newlines, the `Native` style will be used.
163 pub(crate) fn apply(self, formatted_text: &mut String, raw_input_text: &str) {
164 use crate::NewlineStyle::*;
165 let mut style = self;
167 style = Self::auto_detect(raw_input_text);
170 style = Self::native();
174 let mut transformed = String::with_capacity(2 * formatted_text.capacity());
175 for c in formatted_text.chars() {
177 '\n' => transformed.push_str("\r\n"),
179 c => transformed.push(c),
182 *formatted_text = transformed;
185 Native => unreachable!("NewlineStyle::Native"),
186 Auto => unreachable!("NewlineStyle::Auto"),
191 configuration_option_enum! { BraceStyle:
194 // Prefer same line except where there is a where-clause, in which case force
195 // the brace to the next line.
199 configuration_option_enum! { ControlBraceStyle:
200 // K&R style, Rust community default
208 configuration_option_enum! { IndentStyle:
209 // First line on the same line as the opening brace, all lines aligned with
212 // First line is on a new line and all lines align with block indent.
216 configuration_option_enum! { Density:
217 // Fit as much on one line as possible.
221 // Place every item on a separate line.
225 configuration_option_enum! { TypeDensity:
226 // No spaces around "=" and "+"
228 // Spaces around " = " and " + "
232 configuration_option_enum! { Heuristics:
233 // Turn off any heuristics
235 // Turn on max heuristics
237 // Use Rustfmt's defaults
242 pub fn to_list_tactic(self, len: usize) -> ListTactic {
244 Density::Compressed => ListTactic::Mixed,
245 Density::Tall => ListTactic::HorizontalVertical,
246 Density::Vertical if len == 1 => ListTactic::Horizontal,
247 Density::Vertical => ListTactic::Vertical,
252 configuration_option_enum! { ReportTactic:
258 // What Rustfmt should emit. Mostly corresponds to the `--emit` command line
260 configuration_option_enum! { EmitMode:
263 // Writes the output to stdout.
265 // Displays how much of the input file was processed
269 // Output the changed lines (for internal value only)
271 // Checks if a diff can be generated. If so, rustfmt outputs a diff and quits with exit code 1.
272 // This option is designed to be run in CI where a non-zero exit signifies non-standard code
273 // formatting. Used for `--check`.
277 // Client-preference for coloured output.
278 configuration_option_enum! { Color:
279 // Always use color, whether it is a piped or terminal output
283 // Automatically use color, if supported by terminal
287 configuration_option_enum! { Version:
295 /// Whether we should use a coloured terminal.
296 pub fn use_colored_tty(self) -> bool {
298 Color::Always => true,
299 Color::Never => false,
300 Color::Auto => atty::is(atty::Stream::Stdout),
305 // How chatty should Rustfmt be?
306 configuration_option_enum! { Verbosity:
310 // Emit as little as possible.
314 #[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
315 pub struct WidthHeuristics {
316 // Maximum width of the args of a function call before falling back
317 // to vertical formatting.
318 pub fn_call_width: usize,
319 // Maximum width of the args of a function-like attributes before falling
320 // back to vertical formatting.
321 pub attr_fn_like_width: usize,
322 // Maximum width in the body of a struct lit before falling back to
323 // vertical formatting.
324 pub struct_lit_width: usize,
325 // Maximum width in the body of a struct variant before falling back
326 // to vertical formatting.
327 pub struct_variant_width: usize,
328 // Maximum width of an array literal before falling back to vertical
330 pub array_width: usize,
331 // Maximum length of a chain to fit on a single line.
332 pub chain_width: usize,
333 // Maximum line length for single line if-else expressions. A value
334 // of zero means always break if-else expressions.
335 pub single_line_if_else_max_width: usize,
338 impl WidthHeuristics {
339 // Using this WidthHeuristics means we ignore heuristics.
340 pub fn null() -> WidthHeuristics {
342 fn_call_width: usize::max_value(),
343 attr_fn_like_width: usize::max_value(),
345 struct_variant_width: 0,
346 array_width: usize::max_value(),
347 chain_width: usize::max_value(),
348 single_line_if_else_max_width: 0,
352 pub fn set(max_width: usize) -> WidthHeuristics {
354 fn_call_width: max_width,
355 attr_fn_like_width: max_width,
356 struct_lit_width: max_width,
357 struct_variant_width: max_width,
358 array_width: max_width,
359 chain_width: max_width,
360 single_line_if_else_max_width: max_width,
364 // scale the default WidthHeuristics according to max_width
365 pub fn scaled(max_width: usize) -> WidthHeuristics {
366 const DEFAULT_MAX_WIDTH: usize = 100;
367 let max_width_ratio = if max_width > DEFAULT_MAX_WIDTH {
368 let ratio = max_width as f32 / DEFAULT_MAX_WIDTH as f32;
369 // round to the closest 0.1
370 (ratio * 10.0).round() / 10.0
375 fn_call_width: (60.0 * max_width_ratio).round() as usize,
376 attr_fn_like_width: (70.0 * max_width_ratio).round() as usize,
377 struct_lit_width: (18.0 * max_width_ratio).round() as usize,
378 struct_variant_width: (35.0 * max_width_ratio).round() as usize,
379 array_width: (60.0 * max_width_ratio).round() as usize,
380 chain_width: (60.0 * max_width_ratio).round() as usize,
381 single_line_if_else_max_width: (50.0 * max_width_ratio).round() as usize,
386 impl ::std::str::FromStr for WidthHeuristics {
387 type Err = &'static str;
389 fn from_str(_: &str) -> Result<Self, Self::Err> {
390 Err("WidthHeuristics is not parsable")
394 impl Default for EmitMode {
395 fn default() -> EmitMode {
400 /// A set of directories, files and modules that rustfmt should ignore.
401 #[derive(Default, Serialize, Clone, Debug, PartialEq)]
402 pub struct IgnoreList {
403 /// A set of path specified in rustfmt.toml.
405 path_set: HashSet<PathBuf>,
406 /// A path to rustfmt.toml.
407 #[serde(skip_serializing)]
408 rustfmt_toml_path: PathBuf,
411 impl<'de> Deserialize<'de> for IgnoreList {
412 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
414 D: Deserializer<'de>,
416 struct HashSetVisitor;
417 impl<'v> Visitor<'v> for HashSetVisitor {
418 type Value = HashSet<PathBuf>;
420 fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
421 formatter.write_str("a sequence of path")
424 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
428 let mut path_set = HashSet::new();
429 while let Some(elem) = seq.next_element()? {
430 path_set.insert(elem);
436 path_set: deserializer.deserialize_seq(HashSetVisitor)?,
437 rustfmt_toml_path: PathBuf::new(),
442 impl<'a> IntoIterator for &'a IgnoreList {
443 type Item = &'a PathBuf;
444 type IntoIter = hash_set::Iter<'a, PathBuf>;
446 fn into_iter(self) -> Self::IntoIter {
452 pub fn add_prefix(&mut self, dir: &Path) {
453 self.rustfmt_toml_path = dir.to_path_buf();
456 pub fn rustfmt_toml_path(&self) -> &Path {
457 &self.rustfmt_toml_path
461 impl ::std::str::FromStr for IgnoreList {
462 type Err = &'static str;
464 fn from_str(_: &str) -> Result<Self, Self::Err> {
465 Err("IgnoreList is not parsable")
469 /// Maps client-supplied options to Rustfmt's internals, mostly overriding
470 /// values in a config with values from the command line.
471 pub trait CliOptions {
472 fn apply_to(self, config: &mut Config);
473 fn config_path(&self) -> Option<&Path>;
476 // The edition of the compiler (RFC 2052)
477 configuration_option_enum! { Edition:
482 impl Default for Edition {
483 fn default() -> Edition {
489 pub(crate) fn to_libsyntax_pos_edition(self) -> syntax_pos::edition::Edition {
491 Edition::Edition2015 => syntax_pos::edition::Edition::Edition2015,
492 Edition::Edition2018 => syntax_pos::edition::Edition::Edition2018,
498 fn test_newline_style_auto_detect() {
499 let lf = "One\nTwo\nThree";
500 let crlf = "One\r\nTwo\r\nThree";
501 let none = "One Two Three";
503 assert_eq!(NewlineStyle::Unix, NewlineStyle::auto_detect(lf));
504 assert_eq!(NewlineStyle::Windows, NewlineStyle::auto_detect(crlf));
505 assert_eq!(NewlineStyle::Native, NewlineStyle::auto_detect(none));
509 fn test_newline_style_auto_apply() {
510 let auto = NewlineStyle::Auto;
512 let formatted_text = "One\nTwo\nThree";
513 let raw_input_text = "One\nTwo\nThree";
515 let mut out = String::from(formatted_text);
516 auto.apply(&mut out, raw_input_text);
517 assert_eq!("One\nTwo\nThree", &out, "auto should detect 'lf'");
519 let formatted_text = "One\nTwo\nThree";
520 let raw_input_text = "One\r\nTwo\r\nThree";
522 let mut out = String::from(formatted_text);
523 auto.apply(&mut out, raw_input_text);
524 assert_eq!("One\r\nTwo\r\nThree", &out, "auto should detect 'crlf'");
528 let formatted_text = "One\nTwo\nThree";
529 let raw_input_text = "One Two Three";
531 let mut out = String::from(formatted_text);
532 auto.apply(&mut out, raw_input_text);
534 "One\nTwo\nThree", &out,
535 "auto-native-unix should detect 'lf'"
541 let formatted_text = "One\nTwo\nThree";
542 let raw_input_text = "One Two Three";
544 let mut out = String::from(formatted_text);
545 auto.apply(&mut out, raw_input_text);
547 "One\r\nTwo\r\nThree", &out,
548 "auto-native-windows should detect 'crlf'"