Struct simplegen::CodeBuffer [−][src]
pub struct CodeBuffer { /* fields omitted */ }
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
impl CodeBuffer
[src]
impl CodeBuffer
[src]pub fn new(indent: i32) -> Self
[src]
Create a new IndentedWriter
.
Arguments
indent
- Number of spaces to indent by.
Examples
use simplegen::CodeBuffer; let mut buffer = CodeBuffer::new(4);
pub fn println(&mut self, str: &str)
[src]
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!");
pub fn indent_right(&mut self)
[src]
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.
pub fn indent_left(&mut self)
[src]
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.
pub fn println_right(&mut self, str: &str)
[src]
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.
pub fn println_left(&mut self, str: &str)
[src]
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
impl Default for CodeBuffer
[src]
impl Default for CodeBuffer
[src]impl ToString for CodeBuffer
[src]
impl ToString for CodeBuffer
[src]fn to_string(&self) -> String
[src]
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 RefUnwindSafe for CodeBuffer
impl RefUnwindSafe for CodeBuffer
impl Send for CodeBuffer
impl Send for CodeBuffer
impl Sync for CodeBuffer
impl Sync for CodeBuffer
impl Unpin for CodeBuffer
impl Unpin for CodeBuffer
impl UnwindSafe for CodeBuffer
impl UnwindSafe for CodeBuffer