flatten

Function flatten 

Source
pub fn flatten<C: Cost>(x: Doc<C>) -> Doc<C>
Expand description

Create a flattened version of a document.

Flattening converts all newlines within a document to the flat alternative that was provided to newline, producing a document that can be rendered on a single line. If any newlines within the document provided None for their alternative (e.g. hard_nl), then the flattened document will fail to render, forcing the printer to choose another path.

ยงExample

let doc = text("a") & nl() & text("b") & nl() & text("c");
assert_eq!(doc.to_string(), r"a
b
c");

let doc = flatten(doc);
assert_eq!(doc.to_string(), "a b c");

let doc = text("a") & brk() & text("b") & brk() & text("c");
assert_eq!(doc.to_string(), r"a
b
c");

let doc = flatten(doc);
assert_eq!(doc.to_string(), "abc");

let doc = text("a") & hard_nl() & text("b") & hard_nl() & text("c");
assert_eq!(doc.to_string(), r"a
b
c");

let doc = flatten(doc);
assert!(matches!(doc.validate(80), Err(pretty_expressive::Error)));