use std::{fs, path::PathBuf, str};
const REPLACE_PRIMITIVES: (&str, &str, &str, &str) = ("u128", "Bitmap128", "128", "bitmap128");
const WITH_PRIMITIVES: [(&str, &str, &str, &str); 5] = [
("u64", "64", "64", "bitmap64"),
("u32", "32", "32", "bitmap32"),
("u16", "16", "16", "bitmap16"),
("u8", "8", "8", "bitmap8"),
("usize", "Arch", "usize", "bitmap_arch"),
];
const REPLACE_OVERSIZED: (&str, &str, &str, &str) = ("8_192", "BitmapKB", "bitmap_kb", "bitmap_kb");
const WITH_OVERSIZED: [(&str, &str, &str, &str); 5] = [
("256", "256", "bitmap_256", "bitmap_256"),
("512", "512", "bitmap_512", "bitmap_512"),
("1_024", "1024", "bitmap_1024", "bitmap_1024"),
("2_048", "2048", "bitmap_2048", "bitmap_2048"),
("4_096", "4096", "bitmap_4096", "bitmap_4096"),
];
fn create_or_replace(
src_dir_path: String,
replace: (&str, &str, &str, &str),
with: Vec<(&str, &str, &str, &str)>,
) {
let original = match fs::read_to_string(String::from(&src_dir_path) + replace.3 + ".rs") {
Ok(contents) => contents,
Err(e) => panic!(
"Error: could not read file {}, caused by {}",
String::from(&src_dir_path) + replace.3 + ".rs",
e
),
};
for write_values in with {
let path: PathBuf = [".", &src_dir_path, &(String::from(write_values.3) + ".rs")]
.iter()
.collect();
let path = path.as_path();
let mut new_content = String::from(&original).replace(replace.0, write_values.0);
new_content = new_content.replace(replace.1, &(String::from("Bitmap") + write_values.1));
new_content = new_content.replace(replace.2, write_values.2);
match fs::write(path, new_content) {
Ok(_) => {}
Err(error) => {
eprintln!("Could not write to file!");
eprintln!("File path: {}", path.to_str().unwrap());
eprintln!("Caused by: {}", error);
}
}
}
}
fn create_or_replace_primitive_tests() {
create_or_replace(
String::from("./tests/primitives/"),
REPLACE_PRIMITIVES,
Vec::from(WITH_PRIMITIVES),
);
}
fn create_or_replace_primitive_modules() {
create_or_replace(
String::from("./src/primitives/"),
REPLACE_PRIMITIVES,
Vec::from(WITH_PRIMITIVES),
);
}
fn create_or_replace_oversized_modules() {
create_or_replace(
String::from("./src/oversized/"),
REPLACE_OVERSIZED,
Vec::from(WITH_OVERSIZED),
);
}
fn create_or_replace_oversized_tests() {
create_or_replace(
String::from("./tests/oversized/"),
REPLACE_OVERSIZED,
Vec::from(WITH_OVERSIZED),
);
}
fn main() {
create_or_replace_primitive_tests();
create_or_replace_primitive_modules();
create_or_replace_oversized_modules();
create_or_replace_oversized_tests();
}