Skip to content

Instantly share code, notes, and snippets.

@thebluefish
Created February 16, 2024 17:43
Show Gist options
  • Save thebluefish/af0f4a2f27f7bb0c2febfe6f60e6f0a9 to your computer and use it in GitHub Desktop.
Save thebluefish/af0f4a2f27f7bb0c2febfe6f60e6f0a9 to your computer and use it in GitHub Desktop.
use std::fs::File;
use std::io::{BufWriter, Read};
use walkdir::WalkDir;
use foo::Foo;
/// Converts postcard `.post` files to bincode `.bin` files assuming they represent `Foo` data
fn main() -> anyhow::Result<()> {
let root = std::env::current_dir()?.join("data");
for entry in WalkDir::new(&root).sort_by_file_name().into_iter().filter_map(|e| e.ok()) {
let path = entry.into_path();
if !path.is_dir() {
if let Some(ext) = &path.extension() {
if ext.eq_ignore_ascii_case("post") {
println!("converting {}", path.to_string_lossy());
let item: Foo = {
let mut file = File::open(&path)?;
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
postcard::from_bytes(&buf)?
};
// create destination file
let dest = path.with_extension("bin");
std::fs::create_dir_all(&dest.parent().unwrap())?;
// write data in new format
let file = File::create(dest)?;
let mut writer = BufWriter::new(file);
bincode::serialize_into(&mut writer, &item)?;
// delete old file
std::fs::remove_file(path)?;
}
}
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment