omry-archiving 0.4.0

Archiving abstractions for the Omry project.
Documentation
//! This program generates an example [`ArchiveHolder`] and serializes it
//! to a location where flora's test code can use it for integration tests.
use omry_archiving::{Document, Marshal};
use std::env;
use std::fs::{self, File};
use std::io::prelude::*;

/// Environment variable key that corresponds to path of the current Cargo workspace.
const WORKSPACE_DIR_KEY: &str = "CARGO_WORKSPACE_DIR";

/// Path to the test HTML file, relative to CARGO_WORKSPACE_DIR (defined in .cargo/config.toml).
const TEST_WEB_PAGE_SUBPATH: &str = "test/data/example.html";

/// Path to where the output archive should be written to, relative to CARGO_WORKSPACE_DIR.
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(())
}