Skip to main content

Doc

Enum Doc 

Source
pub enum Doc {
    Nil,
    Text(String),
    Line,
    SoftLine,
    HardLine,
    Indent(Box<Doc>),
    Concat(Box<Doc>, Box<Doc>),
    Group(Box<Doc>),
    IfBreak {
        flat: Box<Doc>,
        broken: Box<Doc>,
    },
}
Expand description

Intermediate representation for formatting.

A Doc describes how content should be laid out, with the actual line-breaking decisions deferred to the printer.

Variants§

§

Nil

Empty document

§

Text(String)

Literal text (no line breaks allowed within)

§

Line

A line break: becomes a single space if the enclosing group fits on one line, otherwise becomes a newline followed by current indentation.

§

SoftLine

A soft line break: becomes empty if the enclosing group fits on one line, otherwise becomes a newline followed by current indentation.

§

HardLine

Always a line break, regardless of whether the group fits.

§

Indent(Box<Doc>)

Increase indentation for the nested document.

§

Concat(Box<Doc>, Box<Doc>)

Concatenate two documents.

§

Group(Box<Doc>)

Try to fit the document on one line. If it doesn’t fit within the max width, break at Line and SoftLine positions.

§

IfBreak

Conditional content based on whether the enclosing group breaks.

  • flat: used when the group fits on one line
  • broken: used when the group breaks across lines

Fields

§flat: Box<Doc>
§broken: Box<Doc>

Implementations§

Source§

impl Doc

Source

pub fn text(s: impl Into<String>) -> Doc

Create a text document.

Source

pub fn line() -> Doc

Create a line break (space when flat, newline when broken).

Source

pub fn softline() -> Doc

Create a soft line break (empty when flat, newline when broken).

Source

pub fn hardline() -> Doc

Create a hard line break (always a newline).

Source

pub fn indent(doc: Doc) -> Doc

Indent the given document.

Source

pub fn group(doc: Doc) -> Doc

Create a group that tries to fit on one line.

Source

pub fn concat(self, other: Doc) -> Doc

Concatenate two documents.

Source

pub fn if_break(flat: Doc, broken: Doc) -> Doc

Conditional content based on break state.

Source

pub fn join(docs: impl IntoIterator<Item = Doc>, sep: Doc) -> Doc

Join multiple documents with a separator.

Source

pub fn concat_all(docs: impl IntoIterator<Item = Doc>) -> Doc

Concatenate multiple documents.

Source

pub fn surround(prefix: Doc, content: Doc, suffix: Doc) -> Doc

Wrap content with prefix and suffix.

Trait Implementations§

Source§

impl Clone for Doc

Source§

fn clone(&self) -> Doc

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Doc

Source§

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

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

impl Eq for Doc

Source§

impl PartialEq for Doc

Source§

fn eq(&self, other: &Doc) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Doc

Auto Trait Implementations§

§

impl Freeze for Doc

§

impl RefUnwindSafe for Doc

§

impl Send for Doc

§

impl Sync for Doc

§

impl Unpin for Doc

§

impl UnsafeUnpin for Doc

§

impl UnwindSafe for Doc

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

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.