Printer

Trait Printer 

Source
pub trait Printer: Sized {
    type Token;

Show 24 methods // Required methods fn buf(&self) -> &str; fn buf_mut(&mut self) -> &mut String; fn indent_level(&self) -> usize; fn set_indent(&mut self, level: usize); fn into_string(self) -> String; fn token(&mut self, t: &Self::Token); // Provided methods fn word(&mut self, s: &str) { ... } fn char(&mut self, c: char) { ... } fn space(&mut self) { ... } fn spaces(&mut self, n: usize) { ... } fn tab(&mut self) { ... } fn tabs(&mut self, n: usize) { ... } fn newline(&mut self) { ... } fn add_indent(&mut self) { ... } fn indent_width(&self) -> usize { ... } fn spaces_width(&self) -> usize { ... } fn use_tabs(&self) -> bool { ... } fn indent(&mut self) { ... } fn dedent(&mut self) { ... } fn open_block(&mut self, open: &Self::Token) { ... } fn close_block(&mut self, close: &Self::Token) { ... } fn write<T>(&mut self, value: &T) where T: ToTokens<Printer = Self> { ... } fn write_separated<T, I>( &mut self, items: I, sep: &Self::Token, trailing: bool, newline_after_sep: bool, ) where T: ToTokens<Printer = Self>, I: IntoIterator<Item = T>, <I as IntoIterator>::IntoIter: ExactSizeIterator { ... } fn write_separated_inline<T, I>(&mut self, items: I, sep: &Self::Token) where T: ToTokens<Printer = Self>, I: IntoIterator<Item = T>, <I as IntoIterator>::IntoIter: ExactSizeIterator { ... }
}
Expand description

Trait for building formatted text output.

Printer provides a structured way to generate formatted text with support for indentation, whitespace control, and token formatting. It’s used by ToTokens implementations to produce output.

§Associated Types

  • Token: The token type for grammar-specific formatting

§Required Methods

  • buf(): Get current buffer contents
  • buf_mut(): Get mutable buffer for appending
  • indent_level(): Current indentation depth
  • set_indent(level): Set indentation depth
  • into_string(): Consume and return final output
  • token(t): Format a token (grammar-specific)

§Provided Methods

Basic output:

  • word(s), char(c): Append text
  • space(), spaces(n), tab(), tabs(n): Whitespace
  • newline(): Newline with auto-indent

Indentation:

  • indent(), dedent(): Change indent level
  • open_block(token), close_block(token): Block delimiters

Structured output:

  • write(value): Write a ToTokens value
  • write_separated(items, sep, ...): Write items with separators

§Example

use synkit::Printer;

#[derive(Default)]
struct MyPrinter {
    buf: String,
    indent: usize,
}

impl Printer for MyPrinter {
    type Token = MyTok;

    fn buf(&self) -> &str { &self.buf }
    fn buf_mut(&mut self) -> &mut String { &mut self.buf }
    fn indent_level(&self) -> usize { self.indent }
    fn set_indent(&mut self, level: usize) { self.indent = level; }
    fn into_string(self) -> String { self.buf }

    fn token(&mut self, t: &Self::Token) {
        match t {
            MyTok::Plus => self.word("+"),
            MyTok::Minus => self.word("-"),
            // ...
        }
    }
}

Required Associated Types§

Source

type Token

The token type for grammar-specific formatting.

Required Methods§

Source

fn buf(&self) -> &str

Get the current buffer contents.

Source

fn buf_mut(&mut self) -> &mut String

Get a mutable reference to the buffer for appending.

Source

fn indent_level(&self) -> usize

Get the current indentation level.

Source

fn set_indent(&mut self, level: usize)

Set the indentation level.

Source

fn into_string(self) -> String

Consume the printer and return the final string.

Source

fn token(&mut self, t: &Self::Token)

Format a token to text.

This is grammar-specific and should convert tokens to their textual representation (e.g., Plus"+").

Provided Methods§

Source

fn word(&mut self, s: &str)

Append a string to the buffer.

Source

fn char(&mut self, c: char)

Append a single character to the buffer.

Source

fn space(&mut self)

Append a single space.

Source

fn spaces(&mut self, n: usize)

Append multiple spaces.

Source

fn tab(&mut self)

Append a single tab.

Source

fn tabs(&mut self, n: usize)

Append multiple tabs.

Source

fn newline(&mut self)

Append a newline and auto-indent.

Source

fn add_indent(&mut self)

Add indentation at the current level.

Source

fn indent_width(&self) -> usize

Get the number of spaces per indent level.

Default: 4 spaces

Source

fn spaces_width(&self) -> usize

Calculate total spaces for current indent level.

Source

fn use_tabs(&self) -> bool

Whether to use tabs for indentation.

Default: true (tabs)

Source

fn indent(&mut self)

Increase indentation level by 1.

Source

fn dedent(&mut self)

Decrease indentation level by 1.

Saturates at 0 (won’t go negative).

Source

fn open_block(&mut self, open: &Self::Token)

Open a block: write token, indent, newline.

Source

fn close_block(&mut self, close: &Self::Token)

Close a block: dedent, newline, write token.

Source

fn write<T>(&mut self, value: &T)
where T: ToTokens<Printer = Self>,

Write a value implementing ToTokens.

Source

fn write_separated<T, I>( &mut self, items: I, sep: &Self::Token, trailing: bool, newline_after_sep: bool, )
where T: ToTokens<Printer = Self>, I: IntoIterator<Item = T>, <I as IntoIterator>::IntoIter: ExactSizeIterator,

Write items separated by a delimiter token.

§Arguments
  • items - Iterator of items to write
  • sep - Separator token between items
  • trailing - Whether to add separator after last item
  • newline_after_sep - Whether to add newline after each separator
Source

fn write_separated_inline<T, I>(&mut self, items: I, sep: &Self::Token)
where T: ToTokens<Printer = Self>, I: IntoIterator<Item = T>, <I as IntoIterator>::IntoIter: ExactSizeIterator,

Write items with inline spacing (space after separator, no newlines).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§