Skip to main content

Document

Struct Document 

Source
pub struct Document { /* private fields */ }
Expand description

One pretty-printed document: the owner of its layout options, its content, and its rendering.

Configure the layout with the builder methods (before anything is printed), write content through printer, and render by consuming the document with finish — rendering happens exactly once, at the end. The PrettyPrinter this hands out is write-only, so code that is given a printer (a PrettyPrintable implementation, a PrintableGenerator) can never render or otherwise observe the document it is contributing to.

§Example

use hegel::Document;

let mut doc = Document::new().max_width(10);
let p = doc.printer();
p.begin_group(1, "[");
p.text("first");
p.text(",");
p.breakable(" ");
p.text("second");
p.end_group("]");
assert_eq!(doc.finish(), "[first,\n second]");

Implementations§

Source§

impl Document

Source

pub fn new() -> Self

Create an empty document with the default layout options (a maximum line width of 79 characters).

Source

pub fn max_width(self, max_width: usize) -> Self

Keep lines within max_width characters where the group structure allows it. Defaults to 79.

Layout options describe the whole document, so they must be chosen up front: calling this after printer has been used is an error, as is a max_width of 0.

Source

pub fn printer(&mut self) -> &mut PrettyPrinter

The printer to write this document’s content through.

Source

pub fn finish(self) -> String

Splice any outstanding deferred content into place, lay the document out, and return it.

Consuming the document is what makes rendering a once-at-the-end operation; there is no way to observe a partially built document.

Trait Implementations§

Source§

impl Debug for Document

Source§

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

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

impl Default for Document

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.