1use pretty::RcDoc;
4use std::{borrow::Cow, fmt, ops};
5
6#[derive(Clone)]
12pub struct D<'a>(pub RcDoc<'a, ()>);
13
14impl<'a> ops::Add for D<'a> {
15 type Output = D<'a>;
16
17 fn add(self, rhs: Self) -> Self::Output {
18 D(self.0.append(rhs.0))
19 }
20}
21
22pub fn t<'a, U: Into<Cow<'a, str>>>(data: U) -> D<'a> {
24 D(RcDoc::text(data))
25}
26
27pub fn s<'a>() -> D<'a> {
29 D(RcDoc::line())
30}
31
32pub fn binop<'a>(op: &'a str, l: D<'a>, r: D<'a>) -> D<'a> {
34 ((l + s() + t(op)).group() + (s() + r).indented()).group()
35}
36
37pub fn tuple<'a, I: IntoIterator<Item = D<'a>>>(i: I) -> D<'a> {
39 D(RcDoc::intersperse(i.into_iter().map(|d| d.0.group()), (t(",") + s()).0))
40 .brackets()
41 .group()
42}
43
44impl<'a> D<'a> {
45 pub fn group(self) -> D<'a> {
47 D(self.0.group())
48 }
49
50 pub fn parens(self) -> D<'a> {
52 t("(") + self.group() + t(")")
53 }
54
55 pub fn indented(self) -> Self {
57 D(self.0.nest(2))
58 }
59
60 pub fn brackets(self) -> D<'a> {
62 t("[") + (s() + self + s()).indented() + t("]")
63 }
64
65 pub fn pretty(&self) -> impl fmt::Display {
67 self.0.pretty(80)
68 }
69}