Skip to main content

ic_query/nns/
render.rs

1//! Shared NNS text-rendering primitives for inventory and refresh reports.
2
3use ic_host_text::escape::terminal_text;
4
5#[cfg(feature = "nns-host")]
6use crate::text_value::yes_no;
7
8#[must_use]
9pub fn compact_text(value: &str, chars: usize) -> String {
10    terminal_text(value).chars().take(chars).collect()
11}
12
13#[must_use]
14pub fn text_or_dash(value: Option<&str>) -> String {
15    value.filter(|text| !text.is_empty()).map_or_else(
16        || "-".to_string(),
17        |value| terminal_text(value).into_owned(),
18    )
19}
20
21#[must_use]
22pub fn optional_f32_text(value: Option<f32>) -> String {
23    value.map_or_else(|| "-".to_string(), |value| value.to_string())
24}
25
26#[must_use]
27pub fn optional_node_count_text(value: Option<u32>) -> String {
28    value.map_or_else(|| "unknown".to_string(), |count| count.to_string())
29}
30
31#[cfg(feature = "nns-host")]
32#[must_use]
33/// Render a Registry inventory refresh publication outcome.
34pub fn nns_inventory_refresh_report_text(report: &crate::nns::NnsInventoryRefreshReport) -> String {
35    let mut lines = vec![
36        format!("network: {}", terminal_text(&report.network)),
37        format!("cache_path: {}", terminal_text(&report.cache_path)),
38        format!(
39            "refresh_lock_path: {}",
40            terminal_text(&report.refresh_lock_path)
41        ),
42    ];
43    if let Some(governance_canister_id) = &report.governance_canister_id {
44        lines.push(format!(
45            "governance_canister_id: {}",
46            terminal_text(governance_canister_id)
47        ));
48    }
49    lines.extend([
50        format!("registry_canister_id: {}", report.registry_canister_id),
51        format!("registry_version: {}", report.registry_version),
52        format!("fetched_at: {}", terminal_text(&report.fetched_at)),
53        format!(
54            "source_endpoint: {}",
55            terminal_text(&report.source_endpoint)
56        ),
57        format!("fetched_by: {}", terminal_text(&report.fetched_by)),
58        format!("dry_run: {}", yes_no(report.dry_run)),
59        format!("wrote_cache: {}", yes_no(report.wrote_cache)),
60        format!(
61            "replaced_existing_cache: {}",
62            yes_no(report.replaced_existing_cache)
63        ),
64        format!("collection: {}", terminal_text(&report.collection)),
65        format!("row_count: {}", report.row_count),
66    ]);
67    lines.join("\n")
68}