Expand description
§prettyless (a fork of pretty.rs)
Original work Copyright (c) 2014 Jonathan Sterling and Darin Morrison Modifications Copyright (c) 2025 QuadnucYard
This crate defines a Wadler-style pretty-printing API.
Start with the static functions of Doc.
§Quick start
Let’s pretty-print simple sexps! We want to pretty print sexps like
(1 2 3)or, if the line would be too long, like
((1)
(2 3)
(4 5 6))A simple symbolic expression consists of a numeric atom or a nested ordered list of symbolic expression children.
enum SExp {
Atom(u32),
List(Vec<SExp>),
}
use SExp::*;We define a simple conversion to a Doc. Atoms are rendered as strings; lists are recursively rendered, with spaces between children where appropriate. Children are nested and grouped, allowing them to be laid out in a single line as appropriate.
impl SExp {
/// Return a pretty printed format of self.
pub fn to_doc(&self) -> RcDoc {
match *self {
Atom(ref x) => RcDoc::as_string(x),
List(ref xs) =>
RcDoc::text("(")
.append(RcDoc::intersperse(xs.into_iter().map(|x| x.to_doc()), Doc::line()).nest(1).group())
.append(RcDoc::text(")"))
}
}
}Next, we convert the Doc to a plain old string.
impl SExp {
pub fn to_pretty(&self, width: usize) -> String {
let mut w = Vec::new();
self.to_doc().render(width, &mut w).unwrap();
String::from_utf8(w).unwrap()
}
}And finally we can test that the nesting and grouping behaves as we expected.
let atom = SExp::Atom(5);
assert_eq!("5", atom.to_pretty(10));
let list = SExp::List(vec![SExp::Atom(1), SExp::Atom(2), SExp::Atom(3)]);
assert_eq!("(1 2 3)", list.to_pretty(10));
assert_eq!("\
(1
2
3)", list.to_pretty(5));§Advanced usage
There’s a more efficient pattern that uses the DocAllocator trait, as implemented by BoxAllocator, to allocate DocBuilder instances. See examples/trees.rs for this approach.
§Line width
Layout decisions are local, so the guarantee is: a text node never lands past the width unless the node had no alternative left to take.
- A group is flattened when its own flat content plus the rest of the line fits. The rest of the line is measured with the fallback branch of a union in it, so a group can break earlier than the rendered line needs.
- A union keeps its first branch when that branch’s own output fits, measured from the column the union starts at. Text that follows the union is not part of that decision and can exceed the width; the branch taken as a fallback is never measured.
- A document that has no alternative left renders past the width rather than dropping or splitting text.
Text nodes are never split, reordered or dropped: rendering only inserts spaces, breaks and indentation.
§Indentation
A break’s indentation is written according to
IndentationPolicy, chosen per render call.
Eager, the default and the behavior of upstream pretty, writes it after
the terminator, so blank lines and a trailing break carry trailing spaces;
Deferred postpones it until content on the new line commits it.
Modules§
- combinators
- Allocator-free document combinators.
- prelude
- text
Macros§
- docs
- Concatenates a number of documents (or values that can be converted into a document via the
Prettytrait, like&str)
Structs§
- Arena
- An arena which can be used to allocate
Docvalues. - BoxAllocator
- BoxDoc
- DocBuilder
- The
DocBuildertype allows for convenient appending of documents even for arena allocated documents by storing the arena inline. - FmtWrite
- Writes to something implementing
std::fmt::Write - IoWrite
- Writes to something implementing
std::io::Write - RcAllocator
- RcDoc
- RefDoc
- Newtype wrapper for
&Doc - Render
Options - Configuration for a render pass.
Enums§
- Build
Doc - Either a
Docor a pointer to aDoc(D) - Doc
- The concrete document type. This type is not meant to be used directly. Instead use the static
functions on
Docor the methods on anDocAllocator. - Indentation
Policy - When a structural break writes its indentation.
- Line
Ending - The line terminator emitted for document line breaks when rendering.
Traits§
- DocAllocator
- The
DocAllocatortrait abstracts over a type which can allocate (pointers to)Doc. - DocPtr
- Pretty
- Trait for types which can be converted to a
Document - Render
- Trait representing the operations necessary to render a document