quilt-codegen 0.6.0

Quilt CodeGen — generate code from cells, .qzt to source
Documentation
//! quilt-codegen — generate source code from cells

use quilt_save::Qzt;

pub fn to_python(qzt: &Qzt) -> String {
    let mut s = String::new();
    s.push_str("# Auto-generated by quilt-codegen\n");
    s.push_str("from quilt_cell import Cell\n\n");
    for cell in &qzt.cells {
        s.push_str(&format!("{} = Cell('{}')\n", cell.cell_id.replace('-', "_"), cell.cell_id));
    }
    for (from, to, w) in &qzt.edges {
        s.push_str(&format!("{}.connect({}, weight={})\n", from.replace('-', "_"), to.replace('-', "_"), w));
    }
    s
}

pub fn to_rust(qzt: &Qzt) -> String {
    let mut s = String::new();
    s.push_str("// Auto-generated by quilt-codegen\n");
    s.push_str("use quilt_cell::Cell;\n\n");
    for cell in &qzt.cells {
        s.push_str(&format!("let {} = Cell::new(\"{}\");\n", cell.cell_id.replace('-', "_"), cell.cell_id));
    }
    s
}

pub fn to_typescript(qzt: &Qzt) -> String {
    let mut s = String::new();
    s.push_str("// Auto-generated by quilt-codegen\n");
    s.push_str("import { Cell } from 'quilt-cell';\n\n");
    for cell in &qzt.cells {
        s.push_str(&format!("const {} = new Cell('{}');\n", cell.cell_id.replace('-', "_"), cell.cell_id));
    }
    s
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_codegen() {
        let q = Qzt::new("test");
        let py = to_python(&q);
        assert!(py.contains("quilt_cell"));
    }
}