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]

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]

fn default() -> Self[src]

Create a default implementation of the CodeBuffer with an indentation level of 4 spaces.

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.