Struct DocBuilder

Source
pub struct DocBuilder<'a, D>(pub &'a D, pub BuildDoc<'a, <D as DocAllocator<'a>>::Doc>)
where
    D: DocAllocator<'a> + ?Sized;
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 as DocAllocator<'a>>::Doc>

Implementations§

Source§

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

Source

pub fn into_doc(self) -> <D as DocAllocator<'a>>::Doc

Source

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

Source

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

Append the given document after this document.

Source

pub fn repeat(self, n: usize) -> DocBuilder<'a, D>
where <D as DocAllocator<'a>>::Doc: Clone,

Repeats self n times, appending each repetition.

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = arena.text("[]");

assert_eq!(doc.clone().repeat(0).print(100).to_string(), "");
assert_eq!(arena.len(), 0); // +0

assert_eq!(doc.clone().repeat(1).print(100).to_string(), "[]");
assert_eq!(arena.len(), 0); // +0

assert_eq!(doc.clone().repeat(2).print(100).to_string(), "[][]");
assert_eq!(arena.len(), 1); // +1

assert_eq!(doc.clone().repeat(5).print(100).to_string(), "[][][][][]");
assert_eq!(arena.len(), 5); // +4
Source

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

Make self a line suffix.

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = arena.text(" // comment").as_line_suffix() + arena.text("x");

assert_eq!(doc.print(80).to_string(), "x // comment");
Source

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

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

use prettyless::{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.print(100).to_string(), "let x in x");
assert_eq!(doc.print(8).to_string(), "let x\nx");
Source

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

Equivalent to nil.flat_alt(self)

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = arena.text("flat-only").when_group_flat().group();

assert_eq!(doc.print(0).to_string(), "");
assert_eq!(doc.print(10).to_string(), "flat-only");
Source

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

Equivalent to self.flat_alt(nil)

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = arena.text("a")
    .append(arena.text(",").when_group_break())
    .enclose(arena.line_(), arena.line_())
    .parens()
    .group();

assert_eq!(doc.print(1).to_string(), "(\na,\n)");
assert_eq!(doc.print(10).to_string(), "(a)");
Source

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

Flatten inner docs. Hard line will turn to failure.

Source

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

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>

Increase the indentation level of this document.

Source

pub fn indent(self, offset: usize) -> DocBuilder<'a, D>

Increases the indentation level of this document by the given number of spaces.

This is equivalent to calling nest(offset as isize).

Source

pub fn dedent(self, offset: usize) -> DocBuilder<'a, D>

Decreases the indentation level of this document by the given number of spaces.

This is equivalent to calling nest(-(offset as isize)).

Source

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

Dedents to the root level, which is always 0.

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = (
    arena.text("a")
    + (arena.text("b") + arena.hardline() + arena.text("c")).dedent_to_root()
    + arena.hardline()
    + arena.text("e")
).indent(4);
assert_eq!(doc.print(10).to_string(), "ab\nc\n    e");
Source

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

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

use prettyless::{docs, DocAllocator};

let arena = &prettyless::Arena::new();
let doc = docs![
    arena,
    "lorem",
    " ",
    arena.intersperse(["ipsum", "dolor"].iter().cloned(), arena.line_()).align(),
    arena.hardline(),
    "next",
];
assert_eq!(
    doc.print(80).to_string(),
"
lorem ipsum
      dolor
next".trim_start()
);
Source

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

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = (arena.text("short") + arena.hardline() + arena.text("long long long"))
       .union(arena.text("short") + arena.hardline() + arena.text("short"));
assert_eq!(doc.print(10).to_string(), "short\nshort");
Source

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

Like union, but it only ensures fitting on the first line.

use prettyless::{Arena, DocAllocator};

let arena = Arena::new();
let doc = (arena.text("short") + arena.hardline() + arena.text("long long long"))
       .partial_union(arena.text("short") + arena.hardline() + arena.text("short"));
assert_eq!(doc.print(10).to_string(), "short\nlong long long");
Source

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

Puts self between before and after

Source

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

Source

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

Source

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

Source

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

Source

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

Source

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

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

Source

pub fn is_nil(&self) -> bool

Source

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

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

Source

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

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

Source

pub fn render_raw<W>( &self, width: usize, out: &mut W, ) -> Result<(), <W as Render>::Error>
where W: Render + ?Sized,

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

Source

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

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

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

Trait Implementations§

Source§

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

Source§

type Output = DocBuilder<'a, D>

The resulting type after applying the + operator.
Source§

fn add(self, other: P) -> <DocBuilder<'a, D> as Add<P>>::Output

Performs the + operation. Read more
Source§

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

Source§

fn add_assign(&mut self, other: P)

Performs the += operation. Read more
Source§

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

Source§

fn clone(&self) -> DocBuilder<'a, D>

Returns a duplicate 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> Debug for DocBuilder<'a, D>
where D: DocAllocator<'a> + ?Sized, <D as DocAllocator<'a>>::Doc: Debug,

Source§

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

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

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

Source§

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

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<DocBuilder<'a, D> as Deref>::Target

Dereferences the value.
Source§

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

Source§

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

Converts self into a document

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<'a, D> UnwindSafe for DocBuilder<'a, D>
where D: RefUnwindSafe + ?Sized, <D as DocAllocator<'a>>::Doc: 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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

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>,

Source§

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>,

Source§

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.