<commit_before>use parser::Node;
use std::str;

struct Generator {
    buf: String,
}

impl Generator {
    fn new() -> Generator {
        Generator { buf: String::new() }
    }
    fn write(&mut self, s: &str) {
        self.buf.push_str(s);
    }
    fn result(self) -> String {
        self.buf
    }
}

pub fn generate(ctx_name: &str, tokens: &Vec<Node>) -> String {
    let mut gen = Generator::new();
    gen.write("impl askama::Template for ");
    gen.write(ctx_name);
    gen.write(" {\n");
    gen.write("    fn render(&self) -> String {\n");
    gen.write("        let mut buf = String::new();\n");
    for n in tokens {
        gen.write("        buf.push_str(");
        gen.write(&match n {
            &Node::Lit(val) => format!("{:#?}", str::from_utf8(val).unwrap()),
            &Node::Expr(val) => format!("&self.{}", str::from_utf8(val).unwrap()),
        });
        gen.write(");\n");
    }
    gen.write("        buf");
    gen.write("    }\n");
    gen.write("}\n\n");
    gen.result()
}<commit_msg>Make code generator impl more extensible<commit_after>use parser::Node;
use std::str;

struct Generator {
    buf: String,
}

impl Generator {

    fn new() -> Generator {
        Generator { buf: String::new() }
    }

    fn init(&mut self, name: &str) {
        self.write("impl askama::Template for ");
        self.write(name);
        self.write(" {\n");
        self.write("    fn render(&self) -> String {\n");
        self.write("        let mut buf = String::new();\n");
    }

    fn write(&mut self, s: &str) {
        self.buf.push_str(s);
    }

    fn visit_lit(&mut self, s: &[u8]) {
        self.write("        buf.push_str(");
        self.write(&format!("{:#?}", str::from_utf8(s).unwrap()));
        self.write(");\n");
    }

    fn visit_expr(&mut self, s: &[u8]) {
        self.write("        buf.push_str(");
        self.write(&format!("&self.{}", str::from_utf8(s).unwrap()));
        self.write(");\n");
    }

    fn handle(&mut self, tokens: &Vec<Node>) {
        for n in tokens {
            match n {
                &Node::Lit(val) => { self.visit_lit(val); },
                &Node::Expr(val) => { self.visit_expr(val); },
            }
        }
    }

    fn finalize(&mut self) {
        self.write("        buf");
        self.write("    }\n");
        self.write("}\n\n");
    }

    fn result(self) -> String {
        self.buf
    }

}

pub fn generate(ctx_name: &str, tokens: &Vec<Node>) -> String {
    let mut gen = Generator::new();
    gen.init(ctx_name);
    gen.handle(tokens);
    gen.finalize();
    gen.result()
}
{'fraction_non_alphanumeric': 0.20139157325086973, 'fraction_numerical': 0.0023192887514495554, 'mean_word_length': 2.0482921083627796, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2990203', 'n_tokens_mistral': 892, 'n_tokens_neox': 879, 'n_words': 184}