use ahash::AHashSet as HashSet;
use anyhow::{Result, anyhow};
use payload_dumper::metadata::get_metadata;
use payload_dumper::structs::DeltaArchiveManifest;
use std::path::Path;
use tokio::fs;
use tokio::io::AsyncWriteExt;
pub async fn handle_metadata_extraction(
manifest: &DeltaArchiveManifest,
out_dir: &Path,
data_offset: u64,
mode: &str,
filter_images: &str,
is_stdout: bool,
) -> Result<()> {
let full_mode = mode == "full";
let filter_partitions: Option<HashSet<&str>> = if !filter_images.is_empty() {
Some(filter_images.split(',').collect())
} else {
None
};
let metadata =
get_metadata(manifest, data_offset, full_mode, filter_partitions.as_ref()).await?;
let json_output = serde_json::to_string_pretty(&metadata)
.map_err(|e| anyhow!("Failed to serialize metadata: {}", e))?;
if is_stdout {
let mut stdout = tokio::io::stdout();
stdout
.write_all(json_output.as_bytes())
.await
.map_err(|e| anyhow!("Failed to write metadata to stdout: {}", e))?;
stdout
.flush()
.await
.map_err(|e| anyhow!("Failed to flush stdout: {}", e))?;
} else {
let metadata_file = out_dir.join("metadata.json");
fs::write(&metadata_file, json_output)
.await
.map_err(|e| anyhow!("Failed to write metadata file: {}", e))?;
}
Ok(())
}