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 contentsbuf_mut(): Get mutable buffer for appendingindent_level(): Current indentation depthset_indent(level): Set indentation depthinto_string(): Consume and return final outputtoken(t): Format a token (grammar-specific)
§Provided Methods
Basic output:
word(s),char(c): Append textspace(),spaces(n),tab(),tabs(n): Whitespacenewline(): Newline with auto-indent
Indentation:
indent(),dedent(): Change indent levelopen_block(token),close_block(token): Block delimiters
Structured output:
write(value): Write aToTokensvaluewrite_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§
Required Methods§
Sourcefn indent_level(&self) -> usize
fn indent_level(&self) -> usize
Get the current indentation level.
Sourcefn set_indent(&mut self, level: usize)
fn set_indent(&mut self, level: usize)
Set the indentation level.
Sourcefn into_string(self) -> String
fn into_string(self) -> String
Consume the printer and return the final string.
Provided Methods§
Sourcefn add_indent(&mut self)
fn add_indent(&mut self)
Add indentation at the current level.
Sourcefn indent_width(&self) -> usize
fn indent_width(&self) -> usize
Get the number of spaces per indent level.
Default: 4 spaces
Sourcefn spaces_width(&self) -> usize
fn spaces_width(&self) -> usize
Calculate total spaces for current indent level.
Sourcefn open_block(&mut self, open: &Self::Token)
fn open_block(&mut self, open: &Self::Token)
Open a block: write token, indent, newline.
Sourcefn close_block(&mut self, close: &Self::Token)
fn close_block(&mut self, close: &Self::Token)
Close a block: dedent, newline, write token.
Sourcefn write<T>(&mut self, value: &T)where
T: ToTokens<Printer = Self>,
fn write<T>(&mut self, value: &T)where
T: ToTokens<Printer = Self>,
Write a value implementing ToTokens.
Sourcefn 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<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 writesep- Separator token between itemstrailing- Whether to add separator after last itemnewline_after_sep- Whether to add newline after each separator
Sourcefn 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,
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.