billiards/build.rs

54 lines
1.4 KiB
Rust
Raw Normal View History

2024-08-23 02:40:34 +00:00
use std::env;
use std::fs;
use std::path::Path;
2024-08-25 17:54:06 +00:00
use std::iter::once;
2024-08-23 02:40:34 +00:00
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::<Result<Vec<_>, _>>()
.unwrap();
let mut lines: Vec<String> = 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,
2024-08-25 17:54:06 +00:00
once("vec![")
.chain(lines.iter()
.map(String::as_str)
.chain(once("]")))
.collect::<String>()
2024-08-23 02:40:34 +00:00
).unwrap();
println!("cargo::rerun-if-changed=build.rs");
}