Struct CodeBuffer

Source
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

Source

pub fn new(indent: i32) -> Self

Create a new IndentedWriter.

§Arguments
  • indent - Number of spaces to indent by.
§Examples
use simplegen::CodeBuffer;

let mut buffer = CodeBuffer::new(4);
Source

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!");
Source

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.
Source

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.
Source

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.
Source

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

Source§

fn default() -> Self

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

Source§

impl ToString for CodeBuffer

Source§

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.