use std::env; use std::fs; use std::path::Path; use std::iter::once; fn main() { let out_dir = env::var_os("OUT_DIR").unwrap(); let dest_path = Path::new(&out_dir).join("builtin_tables.rs"); let configs = rusqlite::Connection::open("tables.db") .unwrap() .prepare("SELECT * FROM tables") .unwrap() .query_map([], |row| Ok(( row.get::<_, String>("name")?, row.get::<_, String>("outline")?, row.get::<_, f64>("pos_x")?, row.get::<_, f64>("pos_y")?, row.get::<_, f64>("vec_x")?, row.get::<_, f64>("vec_y")?, ))) .unwrap() .collect::, _>>() .unwrap(); let mut lines: Vec = Vec::new(); for (name, outline, pos_x, pos_y, vec_x, vec_y) in &configs { let outline_parsed: Vec<(f64, f64)> = serde_json::from_str(outline) .expect("outline field should be a valid [(f64, f64)]"); let outline_str: String = outline_parsed .into_iter() .map(|x|format!("({:.15},{:.15}),",x.0,x.1)) .collect(); lines.push(format!( "BilliardConfiguration {{ name: {:?}.into(), outline: vec![{}], initial_pos: ({:.15}, {:.15}), initial_vec: ({:.15}, {:.15}) }},\n", name, &outline_str, *pos_x, *pos_y, *vec_x, *vec_y )); } fs::write( &dest_path, once("vec![") .chain(lines.iter() .map(String::as_str) .chain(once("]"))) .collect::() ).unwrap(); println!("cargo::rerun-if-changed=build.rs"); }