]> git.lizzy.rs Git - rust.git/blobdiff - src/lists.rs
Handle special-case macros
[rust.git] / src / lists.rs
index f3e1c362dc1713c95e42fbbe63089e87328c5f7f..a51653b3732ae37a7404d5c028d7863a0bc33e30 100644 (file)
@@ -160,6 +160,10 @@ pub enum DefinitiveListTactic {
     Vertical,
     Horizontal,
     Mixed,
+    // Special case tactic for `format!()` variants.
+    FormatCall,
+    // Special case tactic for `write!()` varianta.
+    WriteCall,
 }
 
 impl DefinitiveListTactic {
@@ -267,7 +271,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
     I: IntoIterator<Item = T> + Clone,
     T: AsRef<ListItem>,
 {
-    let tactic = formatting.tactic;
+    let mut tactic = formatting.tactic;
     let sep_len = formatting.separator.len();
 
     // Now that we know how we will layout, we can decide for sure if there
@@ -309,6 +313,28 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
             DefinitiveListTactic::Horizontal if !first => {
                 result.push(' ');
             }
+            DefinitiveListTactic::FormatCall if !first => {
+                result.push('\n');
+                result.push_str(indent_str);
+                tactic = DefinitiveListTactic::Horizontal;
+            }
+            DefinitiveListTactic::WriteCall => {
+                let second = i == 1;
+                let third = i == 2;
+
+                if first {
+                    // Nothing
+                } else if second {
+                    result.push('\n');
+                    result.push_str(indent_str);
+                } else if third {
+                    result.push('\n');
+                    result.push_str(indent_str);
+                    tactic = DefinitiveListTactic::Horizontal;
+                } else {
+                    unreachable!();
+                }
+            }
             DefinitiveListTactic::Vertical if !first => {
                 result.push('\n');
                 result.push_str(indent_str);