ic-query-cli 0.102.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: nns::topology::refresh
//!
//! Responsibility: render NNS topology refresh reports as text.
//! Does not own: refresh execution, cache writing, or JSON output.
//! Boundary: formats component refresh outcomes for humans.

use crate::presentation::text_value::field;
use crate::presentation::text_value::table;
use ic_host_text::escape::terminal_text;
use ic_host_text::table::Alignment;

use crate::presentation::text_value::yes_no;
use ic_query::nns::topology::NnsTopologyRefreshReport;

#[must_use]
/// Render the NNS topology refresh report as human-facing text.
pub fn nns_topology_refresh_report_text(report: &NnsTopologyRefreshReport) -> String {
    let mut lines = Vec::new();
    lines.push(format!(
        "topology_refresh: {} components {} wrote {} replaced {} dry_run {}",
        terminal_text(&report.network),
        report.component_count,
        report.wrote_cache_count,
        report.replaced_existing_cache_count,
        yes_no(report.dry_run)
    ));
    lines.push(field("source_endpoint", &report.source_endpoint));
    lines.push(render_refresh_table(report));
    lines.join("\n")
}

fn render_refresh_table(report: &NnsTopologyRefreshReport) -> String {
    let headers = [
        "SOURCE",
        "COUNT",
        "VERSION",
        "FETCHED_AT",
        "WROTE",
        "REPLACED",
        "CACHE",
    ];
    let rows = report
        .components
        .iter()
        .map(|row| {
            [
                row.source.clone(),
                row.item_count.to_string(),
                crate::presentation::text_value::optional_number_text(row.registry_version),
                row.fetched_at.clone(),
                yes_no(row.wrote_cache).to_string(),
                yes_no(row.replaced_existing_cache).to_string(),
                row.cache_path.clone(),
            ]
        })
        .collect::<Vec<_>>();
    let alignments = [
        Alignment::Left,
        Alignment::Right,
        Alignment::Right,
        Alignment::Left,
        Alignment::Left,
        Alignment::Left,
        Alignment::Left,
    ];
    table(&headers, &rows, &alignments)
}