#codegen #println #buffer #indentation #calling #content #text

simplegen

simplegen is a simple text buffer that maintains indentation for calls to println(). The contents of the buffer can be retrieved by calling the to_string() method.

5 releases

0.2.3 Sep 17, 2021
0.2.2 May 1, 2021
0.1.2 Apr 26, 2021

#285 in Template engine

MIT license

12KB
139 lines

simplegen ⚙️

simplegen is a simple code generator library.

simplegen

It is a text buffer that preserves indentation level on calls to println(). The contents of the buffer can be retrieved with to_string().

Example

use simplegen::CodeBuffer;

fn main() {
    let mut buffer = CodeBuffer::new(2);

    buffer.println("[");
    buffer.indent_right();

    for number in 1..=5 {
        buffer.println("{");
        buffer.println_right(format!("\"number\": {}", number).as_str());
        
        if number != 5 {
            buffer.println_left("},");
        } else {
            buffer.println_left("}");
        }
    }
    
    buffer.println_left("]");

    // CodeBuffer::to_string() joins all the lines in the buffer into
    // a single string.
    println!("{}", buffer.to_string());
}

Output

[
  {
    "number": 1
  },
  {
    "number": 2
  },
  {
    "number": 3
  },
  {
    "number": 4
  },
  {
    "number": 5
  }
]

No runtime deps