Crates.io | notugly |
lib.rs | notugly |
version | 0.2.2 |
source | src |
created_at | 2022-08-11 11:35:25.438578 |
updated_at | 2022-08-12 12:35:46.846357 |
description | A simple and generic pretty-printing library |
homepage | |
repository | https://github.com/martin-fl/notugly |
max_upload_size | |
id | 643272 |
size | 23,042 |
Simple and generic pretty-printing library, heavily based on A prettier printer.
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(" ") & y
x / y == x & line() & y
x * y == x & group(line) & y
See the examples/ directory for fully-featured examples.
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));
}