]> git.lizzy.rs Git - rust.git/blobdiff - src/shape.rs
Make children list in-order
[rust.git] / src / shape.rs
index c6a183216b80c2c53e12b91524ea414308980f85..12a911a19315a583d9a62e733758567725cf7ad9 100644 (file)
@@ -27,6 +27,7 @@ pub struct Indent {
 const INDENT_BUFFER_LEN: usize = 80;
 const INDENT_BUFFER: &str =
     "\n                                                                                ";
+
 impl Indent {
     pub fn new(block_indent: usize, alignment: usize) -> Indent {
         Indent {
@@ -181,7 +182,7 @@ pub fn legacy(width: usize, indent: Indent) -> Shape {
 
     pub fn indented(indent: Indent, config: &Config) -> Shape {
         Shape {
-            width: config.max_width().checked_sub(indent.width()).unwrap_or(0),
+            width: config.max_width().saturating_sub(indent.width()),
             indent,
             offset: indent.alignment,
         }
@@ -189,22 +190,11 @@ pub fn indented(indent: Indent, config: &Config) -> Shape {
 
     pub fn with_max_width(&self, config: &Config) -> Shape {
         Shape {
-            width: config
-                .max_width()
-                .checked_sub(self.indent.width())
-                .unwrap_or(0),
+            width: config.max_width().saturating_sub(self.indent.width()),
             ..*self
         }
     }
 
-    pub fn offset(width: usize, indent: Indent, offset: usize) -> Shape {
-        Shape {
-            width,
-            indent,
-            offset,
-        }
-    }
-
     pub fn visual_indent(&self, extra_width: usize) -> Shape {
         let alignment = self.offset + extra_width;
         Shape {
@@ -274,20 +264,22 @@ pub fn used_width(&self) -> usize {
     pub fn rhs_overhead(&self, config: &Config) -> usize {
         config
             .max_width()
-            .checked_sub(self.used_width() + self.width)
-            .unwrap_or(0)
+            .saturating_sub(self.used_width() + self.width)
     }
 
     pub fn comment(&self, config: &Config) -> Shape {
         let width = min(
             self.width,
-            config
-                .comment_width()
-                .checked_sub(self.indent.width())
-                .unwrap_or(0),
+            config.comment_width().saturating_sub(self.indent.width()),
         );
         Shape { width, ..*self }
     }
+
+    pub fn to_string_with_newline(&self, config: &Config) -> Cow<'static, str> {
+        let mut offset_indent = self.indent;
+        offset_indent.alignment = self.offset;
+        offset_indent.to_string_inner(config, 0)
+    }
 }
 
 #[cfg(test)]