Simple code generation library.
This repository has been archived on 2026-09-15. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2021-09-17 17:04:20 +01:00
.github/workflows Create rust.yml 2021-04-26 19:11:46 +01:00
src Rename IndentedPrinter to CodeBuffer and add Examples. 2021-05-01 23:19:49 +01:00
.gitignore Initial implementation of simplegen. 2021-04-26 18:31:16 +01:00
Cargo.toml change license to MIT and bump version to 0.2.3 2021-09-17 17:04:20 +01:00
LICENCE.md change license to MIT and bump version to 0.2.3 2021-09-17 17:04:20 +01:00
README.md Add CI badge to README.md 2021-05-02 00:26:33 +01:00

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
  }
]