Expand description
§notugly
Simple and generic pretty-printing library, heavily based on A prettier printer.
§How to use
To define pretty-printing for a type, implement the Format trait using the various utility functions:
- nil for a null element
- text to create a string into a document
- nest to indent a document
- cat to concatenate two documents
- group and group_with to add the flattened layout as an alternative.
- fold, spread, stack and fill to collapse a list of documents in various ways
To make it easier to define a structure, some operators are defined:
x & y == cat(x,y)x + y == x & text(" ") & yx / y == x & line() & yx * y == x & group(line) & y
See the examples/ directory for fully-featured examples.
§Example : S-Expressions
use notugly::*;
enum SExpr {
Number(i32),
Call(String, Vec<SExpr>),
}
impl Format for SExpr {
fn format(&self) -> Document {
match self {
SExpr::Number(n) => text(format!("{n}")),
SExpr::Call(name, v) => group_with(
"",
group(text("(") & text(name) & nest(2, line() & stack(v))) / text(")"),
),
}
}
}
fn main() {
let big_eq = sexpr!(add (mul 2 6) (div (mul 4 (mul 3 2 1)) (add 1 (sub 3 (add 1 1)))));
println!("{}", big_eq.pretty(40));
}Structs§
- Document
- Opaque type representating a set of possible layouts for a document.
Traits§
- Format
- Given the
formatmethod, telling how to turn aSelfinto a Document, provides theprettymethod to choose the best layout.
Functions§
- bracket
- Convenience function for the common operation of delimiting a document.
- cat
- Concatenates two documents.
- fill
- Collapses a list of document and recursively adds the alternative layouts of using a space or a newline between each document
- fold
- Collapses a list of documents according to
op. If the slice is empty, returns nil. - group
- Adds the flattened layout (everything compressed on one line) as an alternative layout to a document.
- group_
with - Adds the flattened layout (everything compressed on one line, newlines being replace by the
cstring) as an alternative layout to a document. - line
- Produces a newline marker.
- nest
- Indent the given document with
ispaces. - nil
- Produces a null document.
- spread
- Collapses a list of documents and inserts a space between every element of the slice.
- stack
- Collapses a list of documents and inserts a newline between every element of the slice.
- text
- Transforms text into a document.