pub struct CodeBuffer { /* private fields */ }
Expand description
A CodeBuffer is a simple tool that’s useful for basic code generation.
It’s a text buffer that maintains indentation level when writing new lines. You retrieve the correctly indented contents of the buffer by calling CodeBuffer::to_string().
§Example
use simplegen::CodeBuffer;
// The CodeBuffer must be mutable as the buffer maintains state in a
// struct.
let mut code_buffer = CodeBuffer::new(4);
code_buffer.println("fn add_one(x: u64) -> u64) {");
code_buffer.println_right("x + 1");
code_buffer.println_left("}");
let code_string = code_buffer.to_string();
println!("{}", code_string);
// Prints:
// fn add_one(x: u64) -> u64 {
// x + 1
// }
Implementations§
Source§impl CodeBuffer
impl CodeBuffer
Sourcepub fn println(&mut self, str: &str)
pub fn println(&mut self, str: &str)
Write a line to the internal buffer at the current indentation level.
§Arguments
str
- String to append to the buffer.
§Examples
use simplegen::CodeBuffer;
let mut buffer = CodeBuffer::default();
// Calling this will append "Hello, World!" to the buffer at the current
// indentation level, in this case an indentation level of 0.
buffer.println("Hello, World!");
Sourcepub fn indent_right(&mut self)
pub fn indent_right(&mut self)
Indent the internal buffer right.
§Examples
use simplegen::CodeBuffer;
let mut buffer = CodeBuffer::default();
// Increases indentation level by one.
buffer.indent_right();
buffer.println("Hello, World!");
// The string " Hello, World!" gets appended to the buffer, as we
// have increased indentation level by one, and default indentation
// level is 4 spaces.
Sourcepub fn indent_left(&mut self)
pub fn indent_left(&mut self)
Indent the internal buffer left.
§Examples
use simplegen::CodeBuffer;
let mut buffer = CodeBuffer::default();
// We set the indentation level to 1 for demonstration purposes.
buffer.indent_right();
buffer.indent_left();
// The indentation level has returned to 0.
buffer.println("Hello, World!");
// The string "Hello, World!" is appended to the buffer, as we set the
// indentation level from 1, to 0.
Sourcepub fn println_right(&mut self, str: &str)
pub fn println_right(&mut self, str: &str)
Indent right then print a string to the internal buffer.
§Arguments
str
- String to append to the buffer.
§Examples
use simplegen::CodeBuffer;
let mut buffer = CodeBuffer::default();
buffer.println_right("Hello, World!");
// The string " Hello, World!" is appended to the buffer, as the
// CodeBuffer::println_right() function will first increment the
// indentation level, then append the indented string to the buffer.
Sourcepub fn println_left(&mut self, str: &str)
pub fn println_left(&mut self, str: &str)
Indent left then print a string to the internal buffer.
§Arguments
str
- String to append to the buffer.
§Examples
use simplegen::CodeBuffer;
let mut buffer = CodeBuffer::default();
buffer.indent_right();
// Set the indentation level to 1.
buffer.println_left("Hello, World!");
// Indentation level will be returned to 0, so "Hello, World!" will be
// appended to the buffer.
Trait Implementations§
Source§impl Default for CodeBuffer
impl Default for CodeBuffer
Source§impl ToString for CodeBuffer
impl ToString for CodeBuffer
Source§fn to_string(&self) -> String
fn to_string(&self) -> String
Retrieve a string of the internal state of the printer. This will be a string that has been formatted with correct indentation levels
§Examples
use simplegen::CodeBuffer;
let mut buffer = CodeBuffer::default();
buffer.println("[");
buffer.indent_right();
for number in 1..10 {
buffer.println("{");
buffer.println_right(format!("\"number\": {}", number).as_str());
buffer.println_left("},");
}
buffer.println_left("]");
// CodeBuffer::to_string() joins all the lines in the buffer into
// a single string.
println!("{}", buffer.to_string());
Auto Trait Implementations§
impl Freeze for CodeBuffer
impl RefUnwindSafe for CodeBuffer
impl Send for CodeBuffer
impl Sync for CodeBuffer
impl Unpin for CodeBuffer
impl UnwindSafe for CodeBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more