Skip to main content

quilt_codegen/
lib.rs

1//! quilt-codegen — generate source code from cells
2
3use quilt_save::Qzt;
4
5pub fn to_python(qzt: &Qzt) -> String {
6    let mut s = String::new();
7    s.push_str("# Auto-generated by quilt-codegen\n");
8    s.push_str("from quilt_cell import Cell\n\n");
9    for cell in &qzt.cells {
10        s.push_str(&format!("{} = Cell('{}')\n", cell.cell_id.replace('-', "_"), cell.cell_id));
11    }
12    for (from, to, w) in &qzt.edges {
13        s.push_str(&format!("{}.connect({}, weight={})\n", from.replace('-', "_"), to.replace('-', "_"), w));
14    }
15    s
16}
17
18pub fn to_rust(qzt: &Qzt) -> String {
19    let mut s = String::new();
20    s.push_str("// Auto-generated by quilt-codegen\n");
21    s.push_str("use quilt_cell::Cell;\n\n");
22    for cell in &qzt.cells {
23        s.push_str(&format!("let {} = Cell::new(\"{}\");\n", cell.cell_id.replace('-', "_"), cell.cell_id));
24    }
25    s
26}
27
28pub fn to_typescript(qzt: &Qzt) -> String {
29    let mut s = String::new();
30    s.push_str("// Auto-generated by quilt-codegen\n");
31    s.push_str("import { Cell } from 'quilt-cell';\n\n");
32    for cell in &qzt.cells {
33        s.push_str(&format!("const {} = new Cell('{}');\n", cell.cell_id.replace('-', "_"), cell.cell_id));
34    }
35    s
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    #[test]
42    fn test_codegen() {
43        let q = Qzt::new("test");
44        let py = to_python(&q);
45        assert!(py.contains("quilt_cell"));
46    }
47}