Struct pretty::DocBuilder

source ·
pub struct DocBuilder<'a, D, A = ()>(pub &'a D, pub BuildDoc<'a, D::Doc, A>)
where
    D: ?Sized + DocAllocator<'a, A>;
Expand description

The DocBuilder type allows for convenient appending of documents even for arena allocated documents by storing the arena inline.

Tuple Fields§

§0: &'a D§1: BuildDoc<'a, D::Doc, A>

Implementations§

source§

impl<'a, D, A> DocBuilder<'a, D, A>
where A: 'a, D: ?Sized + DocAllocator<'a, A>,

source

pub fn append<E>(self, that: E) -> DocBuilder<'a, D, A>
where E: Pretty<'a, D, A>,

Append the given document after this document.

source

pub fn flat_alt<E>(self, that: E) -> DocBuilder<'a, D, A>
where E: Pretty<'a, D, A>,

Acts as self when laid out on multiple lines and acts as that when laid out on a single line.

use pretty::{Arena, DocAllocator};

let arena = Arena::<()>::new();
let body = arena.line().append("x");
let doc = arena.text("let")
    .append(arena.line())
    .append("x")
    .group()
    .append(
        body.clone()
            .flat_alt(
                arena.line()
                    .append("in")
                    .append(body)
            )
    )
    .group();

assert_eq!(doc.1.pretty(100).to_string(), "let x in x");
assert_eq!(doc.1.pretty(8).to_string(), "let x\nx");
source

pub fn group(self) -> DocBuilder<'a, D, A>

Mark this document as a group.

Groups are layed out on a single line if possible. Within a group, all basic documents with several possible layouts are assigned the same layout, that is, they are all layed out horizontally and combined into a one single line, or they are each layed out on their own line.

source

pub fn nest(self, offset: isize) -> DocBuilder<'a, D, A>

Increase the indentation level of this document.

source

pub fn annotate(self, ann: A) -> DocBuilder<'a, D, A>

source

pub fn union<E>(self, other: E) -> DocBuilder<'a, D, A>
where E: Into<BuildDoc<'a, D::Doc, A>>,

source

pub fn align(self) -> DocBuilder<'a, D, A>
where DocBuilder<'a, D, A>: Clone,

Lays out self so with the nesting level set to the current column

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::{docs, DocAllocator};

let arena = &pretty::Arena::<()>::new();
let doc = docs![
    arena,
    "lorem",
    " ",
    arena.intersperse(["ipsum", "dolor"].iter().cloned(), arena.line_()).align(),
    arena.hardline(),
    "next",
];
assert_eq!(doc.1.pretty(80).to_string(), "lorem ipsum\n      dolor\nnext");
source

pub fn hang(self, adjust: isize) -> DocBuilder<'a, D, A>
where DocBuilder<'a, D, A>: Clone,

Lays out self with a nesting level set to the current level plus adjust.

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena.text("prefix").append(arena.text(" "))
    .append(arena.reflow("Indenting these words with nest").hang(4));
assert_eq!(
    doc.1.pretty(24).to_string(),
    "prefix Indenting these\n           words with\n           nest",
);
source

pub fn indent(self, adjust: usize) -> DocBuilder<'a, D, A>
where DocBuilder<'a, D, A>: Clone,

Indents self by adjust spaces from the current cursor position

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena.text("prefix").append(arena.text(" "))
    .append(arena.reflow("The indent function indents these words!").indent(4));
assert_eq!(
    doc.1.pretty(24).to_string(),
"
prefix     The indent
           function
           indents these
           words!".trim_start(),
);
source

pub fn width(self, f: impl Fn(isize) -> D::Doc + 'a) -> DocBuilder<'a, D, A>
where BuildDoc<'a, D::Doc, A>: Clone,

Lays out self and provides the column width of it available to f

NOTE: The doc pointer type, D may need to be cloned. Consider using cheaply cloneable ptr like RefDoc or RcDoc

use pretty::DocAllocator;

let arena = pretty::Arena::<()>::new();
let doc = arena.text("prefix ")
    .append(arena.column(|l| {
        arena.text("| <- column ").append(arena.as_string(l)).into_doc()
    }));
assert_eq!(doc.1.pretty(80).to_string(), "prefix | <- column 7");
source

pub fn enclose<E, F>(self, before: E, after: F) -> DocBuilder<'a, D, A>
where E: Pretty<'a, D, A>, F: Pretty<'a, D, A>,

Puts self between before and after

source

pub fn single_quotes(self) -> DocBuilder<'a, D, A>

source

pub fn double_quotes(self) -> DocBuilder<'a, D, A>

source

pub fn parens(self) -> DocBuilder<'a, D, A>

source

pub fn angles(self) -> DocBuilder<'a, D, A>

source

pub fn braces(self) -> DocBuilder<'a, D, A>

source

pub fn brackets(self) -> DocBuilder<'a, D, A>

source

pub fn into_doc(self) -> D::Doc

Methods from Deref<Target = Doc<'a, D::Doc, A>>§

source

pub fn render<W>(&self, width: usize, out: &mut W) -> Result<()>
where W: ?Sized + Write,

Writes a rendered document to a std::io::Write object.

source

pub fn render_fmt<W>(&self, width: usize, out: &mut W) -> Result
where W: ?Sized + Write,

Writes a rendered document to a std::fmt::Write object.

source

pub fn render_raw<W>(&self, width: usize, out: &mut W) -> Result<(), W::Error>
where for<'b> W: RenderAnnotated<'b, A> + ?Sized,

Writes a rendered document to a RenderAnnotated<A> object.

source

pub fn pretty<'d>(&'d self, width: usize) -> PrettyFmt<'a, 'd, T, A>

Returns a value which implements std::fmt::Display

use pretty::{Doc, BoxDoc};
let doc = BoxDoc::<()>::group(
    BoxDoc::text("hello").append(Doc::line()).append(Doc::text("world"))
);
assert_eq!(format!("{}", doc.pretty(80)), "hello world");

Trait Implementations§

source§

impl<'a, D, A, P> Add<P> for DocBuilder<'a, D, A>
where D: ?Sized + DocAllocator<'a, A>, P: Pretty<'a, D, A>,

§

type Output = DocBuilder<'a, D, A>

The resulting type after applying the + operator.
source§

fn add(self, other: P) -> Self::Output

Performs the + operation. Read more
source§

impl<'a, D, A, P> AddAssign<P> for DocBuilder<'a, D, A>
where D: ?Sized + DocAllocator<'a, A>, P: Pretty<'a, D, A>,

source§

fn add_assign(&mut self, other: P)

Performs the += operation. Read more
source§

impl<'a, A, D> Clone for DocBuilder<'a, D, A>
where A: Clone, D: DocAllocator<'a, A> + 'a, D::Doc: Clone,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, D, A> Debug for DocBuilder<'a, D, A>
where D: ?Sized + DocAllocator<'a, A>, D::Doc: Debug, A: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, D, A> Deref for DocBuilder<'a, D, A>
where D: ?Sized + DocAllocator<'a, A>,

§

type Target = Doc<'a, <D as DocAllocator<'a, A>>::Doc, A>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<'a, D, A> From<DocBuilder<'a, D, A>> for BuildDoc<'a, D::Doc, A>
where D: ?Sized + DocAllocator<'a, A>,

source§

fn from(val: DocBuilder<'a, D, A>) -> Self

Converts to this type from the input type.
source§

impl<'a, D, A> Pretty<'a, D, A> for DocBuilder<'a, D, A>
where A: 'a, D: ?Sized + DocAllocator<'a, A>,

source§

fn pretty(self, _: &'a D) -> DocBuilder<'a, D, A>

Converts self into a document

Auto Trait Implementations§

§

impl<'a, D, A> Freeze for DocBuilder<'a, D, A>
where <D as DocAllocator<'a, A>>::Doc: Freeze, A: Freeze, <<D as DocAllocator<'a, A>>::Doc as DocPtr<'a, A>>::ColumnFn: Freeze, D: ?Sized,

§

impl<'a, D, A> RefUnwindSafe for DocBuilder<'a, D, A>
where D: RefUnwindSafe + ?Sized, <D as DocAllocator<'a, A>>::Doc: RefUnwindSafe, A: RefUnwindSafe, <<D as DocAllocator<'a, A>>::Doc as DocPtr<'a, A>>::ColumnFn: RefUnwindSafe,

§

impl<'a, D, A> Send for DocBuilder<'a, D, A>
where D: Sync + ?Sized, <D as DocAllocator<'a, A>>::Doc: Send, A: Send, <<D as DocAllocator<'a, A>>::Doc as DocPtr<'a, A>>::ColumnFn: Send,

§

impl<'a, D, A> Sync for DocBuilder<'a, D, A>
where D: Sync + ?Sized, <D as DocAllocator<'a, A>>::Doc: Sync, A: Sync, <<D as DocAllocator<'a, A>>::Doc as DocPtr<'a, A>>::ColumnFn: Sync,

§

impl<'a, D, A> Unpin for DocBuilder<'a, D, A>
where <D as DocAllocator<'a, A>>::Doc: Unpin, A: Unpin, <<D as DocAllocator<'a, A>>::Doc as DocPtr<'a, A>>::ColumnFn: Unpin, D: ?Sized,

§

impl<'a, D, A> UnwindSafe for DocBuilder<'a, D, A>
where D: RefUnwindSafe + ?Sized, <D as DocAllocator<'a, A>>::Doc: UnwindSafe, A: UnwindSafe, <<D as DocAllocator<'a, A>>::Doc as DocPtr<'a, A>>::ColumnFn: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.