use omry_archiving::{Document, Marshal};
use std::env;
use std::fs::{self, File};
use std::io::prelude::*;
const WORKSPACE_DIR_KEY: &str = "CARGO_WORKSPACE_DIR";
const TEST_WEB_PAGE_SUBPATH: &str = "test/data/example.html";
const OUTPUT_ARCHIVE_SUBPATH: &str = "test/data/example.bincode";
#[allow(clippy::print_stdout)]
fn main() -> anyhow::Result<()> {
let (_, workspace_dir_path) = env::vars()
.find(|(k, _)| k == WORKSPACE_DIR_KEY)
.ok_or_else(|| anyhow::anyhow!("{WORKSPACE_DIR_KEY} is not set"))?;
let workspace_dir_path = fs::canonicalize(workspace_dir_path)?;
let web_page_path = workspace_dir_path.join(TEST_WEB_PAGE_SUBPATH);
println!(
"Loading the web page from {}",
web_page_path.to_string_lossy()
);
let mut web_page = File::open(&web_page_path)?;
let mut web_page_data = Vec::new();
web_page.read_to_end(&mut web_page_data)?;
let document = Document::new_web_page_unchecked(web_page_data);
let bytes = document.to_byte_vec()?;
let output_path = workspace_dir_path.join(OUTPUT_ARCHIVE_SUBPATH);
let mut file = File::create(&output_path)?;
file.write_all(&bytes)?;
println!("Wrote output archive to {}", output_path.to_string_lossy());
Ok(())
}