swh-mosaic 0.3.1

MOdular Storage of Archived and Indexed Contents from Software Heritage
Documentation
// Copyright (C) 2026  The Software Heritage developers
// See the AUTHORS file at the top-level directory of this distribution
// License: GNU General Public License version 3, or any later version
// See top-level LICENSE file for more information

//! implementation of the `swh-mosaic info` subcommand

use anyhow::Result;
use std::{path::Path, sync::Arc};

use crate::{backends::MmapMosaicBackend, reader::MosaicReader};

/// Print a summary of a MOSAIC file's contents and indexes
pub fn info(filename: &Path, comment_indices: &[usize]) -> Result<()> {
    let backend = Arc::new(MmapMosaicBackend::new(filename)?);
    let reader = MosaicReader::with_backend(filename, backend.clone())?;
    let indexes = reader.list_indexes()?;
    if comment_indices.is_empty() {
        println!(
            "{}: MOSAICv{} containing {} objects (compression: {}), {} bytes uncompressed, {} comments attached, {} indexes",
            filename.display(),
            reader.doctype_version,
            reader.objects_counter,
            reader.compression,
            reader.objects_total_size,
            reader.comments.len(),
            indexes.len(),
        );
        for index in indexes {
            println!("- {}", index.0);
        }
    } else {
        for &i in comment_indices {
            if let Some(comment) = reader.comments.get(i.saturating_sub(1)) {
                println!("{}", comment);
            } else {
                println!(
                    "Comment {} does not exist: {} has only {} comments.",
                    i,
                    filename.display(),
                    reader.comments.len()
                );
            }
        }
    }
    Ok(())
}